<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Leo Batan</title>
    <description>The latest articles on DEV Community by Leo Batan (@bata0020).</description>
    <link>https://dev.to/bata0020</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F725913%2F8843045e-1770-40f1-b841-24e3116d36f1.jpeg</url>
      <title>DEV Community: Leo Batan</title>
      <link>https://dev.to/bata0020</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bata0020"/>
    <language>en</language>
    <item>
      <title>String.prototype.padEnd() and String.prototype.padStart()</title>
      <dc:creator>Leo Batan</dc:creator>
      <pubDate>Fri, 15 Oct 2021 01:14:00 +0000</pubDate>
      <link>https://dev.to/bata0020/stringprototypepadend-and-stringprototypepadstart-3cke</link>
      <guid>https://dev.to/bata0020/stringprototypepadend-and-stringprototypepadstart-3cke</guid>
      <description>&lt;p&gt;&lt;strong&gt;Goal:&lt;/strong&gt; To understand and learn how padding a string in JavaScript works.&lt;/p&gt;

&lt;h3&gt;
  
  
  String.prototype.padEnd()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;padEnd()&lt;/code&gt; is a string method which pads the &lt;strong&gt;end&lt;/strong&gt; of the current string with a &lt;strong&gt;strPadding&lt;/strong&gt; up to a &lt;strong&gt;specified length&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;padEnd(specifiedLength)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;padEnd(specifiedLength, strPadding)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;specifiedLength&lt;/code&gt; is the length of the resulting string once the padding has been applied to the current string. Note that if this value is less than the current string's length there will be no visible change.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;strPadding&lt;/code&gt;  is the specific string that will be applied to pad the end of the current string. This parameter is optional. Note that if the length of this string padding is greater than the &lt;code&gt;specifiedLength&lt;/code&gt;, it will be cut or shortened to fit within the &lt;code&gt;specifiedLength&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sayIt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Please&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sayIt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;padEnd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;       &lt;span class="c1"&gt;//"Please      "&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sayIt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;padEnd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;   &lt;span class="c1"&gt;//"Please******"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sayIt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;padEnd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;***&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;//"Please**"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sayIt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;padEnd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;        &lt;span class="c1"&gt;//"Please"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Since there was no specified &lt;code&gt;strPadding&lt;/code&gt; on the first example, spaces are applied instead.
&lt;/h6&gt;

&lt;h3&gt;
  
  
  String.prototype.padStart()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;padStart()&lt;/code&gt; is another string method which pads the current string and works similarly with &lt;code&gt;padEnd()&lt;/code&gt; but on this method the padding is applied at the beginning of the current string. A &lt;code&gt;strPadding&lt;/code&gt; will be applied to pad the &lt;strong&gt;start&lt;/strong&gt; of the &lt;strong&gt;current string&lt;/strong&gt; to meet the &lt;code&gt;specifiedLength&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;padStart(specifiedLength)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;padStart(specifiedLength, strPadding)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;callMe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Maybe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callMe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;        &lt;span class="c1"&gt;//"     Maybe"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callMe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;     &lt;span class="c1"&gt;//"*Maybe"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callMe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;   &lt;span class="c1"&gt;//"12Maybe"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callMe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;         &lt;span class="c1"&gt;//"Maybe"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  On example 3, the length of our current string is 5 and we wanted to pad the start with &lt;code&gt;'123'&lt;/code&gt;. Since the length of the resulting string is 7, only &lt;code&gt;'12'&lt;/code&gt; was applied at the start and the last one which is &lt;code&gt;'3'&lt;/code&gt; was cut off.
&lt;/h6&gt;

&lt;p&gt;Again it is important to note the relationship between &lt;code&gt;specifiedLength&lt;/code&gt; and &lt;code&gt;strPadding&lt;/code&gt; and with the current string. If the &lt;code&gt;specifiedLength&lt;/code&gt; is less than the length of the current string then there will be no change to the resulting string and if the length of &lt;code&gt;strPadding&lt;/code&gt; is way too long than the &lt;code&gt;specifiedLength&lt;/code&gt; then it will be cut or shortened to fit in the criteria of the &lt;code&gt;specifiedLength&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This concludes the tutorial about the &lt;code&gt;padEnd()&lt;/code&gt; and &lt;code&gt;padStart()&lt;/code&gt; methods of a string in JavaScript. I hope you enjoyed reading and that it helped you understand how these methods work. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
