JavaScript | padStart() Method

The padStart() method in JavaScript is used to pad a string with another string until it reaches the given length. The padding is applied from the left end of the string.

Syntax:

string.padStart(targetLength, padString)

Parameters: This method accepts two parameters as mentioned above and described below:

  • targetLength: It is the length of the final string once the original string has been padded. If the value is less than the original string length, then the original string is returned.
  • padString: It is the string that is to be padded with the original string. If this value is too long to be within the targetLength, it truncated from the end. The default used string is the space character (” “).

Return Value: It returns the final string that is padded with the given string to the given length.

Example 1: This example uses padStart() method to pad strings into another string.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript | padStart() method
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
  
    <b>JavaScript | padStart() method</b>
  
    <p>Output for "Hello123" with "$": 
        <span class="output"></span>
    </p>
  
    <p>Output for "Geeks" with "XX": 
        <span class="output_2"></span>
    </p>
      
    <button onclick="padStrings()">
        Pad Strings
    </button>
  
    <script type="text/javascript">
        function padStrings() {
            exString = "Hello123";
            exString2 = "Geeks";
  
            prepended_out = 
                exString.padStart(10, "$");
            prepended_out2 = 
                exString2.padStart(10, "XX");
          
            document.querySelector('.output').textContent
                        = prepended_out;
              
            document.querySelector('.output_2').textContent
                        = prepended_out2;
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
    strings-before
  • After clicking the button:
    strings-after

Example 2: This example uses padStart() method to pad numbers into the another number.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript | padStart() method
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
  
    <b>JavaScript | padStart() method</b>
  
    <p>Output for "999" with "0": 
        <span class="output"></span>
    </p>
  
    <p>Output for "123" with "**": 
        <span class="output_2"></span>
    </p>
  
    <button onclick="padNumbers()">
        Pad Numbers
    </button>
  
    <script type="text/javascript">
        function padNumbers() {
            exNumber = 999;
            exNumber2 = 123;
  
            prepended_out
                = String(exNumber).padStart(5, "0");
              
            prepended_out2
                = String(exNumber2).padStart(5, "**");
  
            document.querySelector('.output').textContent
                = prepended_out;
              
            document.querySelector('.output_2').textContent
                = prepended_out2;
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
    numbers-before
  • After clicking the button:
    numbers-after

Supported Browsers: The browser supported by padStart() method are listed below:

  • Google Chrome 57
  • Firefox 48
  • Edge 15
  • Safari 10
  • Opera 44

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.