78 of 264 menu

padStart method

The padStart method pads the start of the current string with another string (several times if necessary) up to the specified length. The first parameter in the method is a desired length of the string, and the second optional parameter is the string with which it will be filled.

Syntax

string.padStart(string length, [string for padding]);

Example

Let's pad the string 'ab' so that its length is 4 characters:

let res = 'ab'.padStart(4); console.log(res);

The code execution result:

' ab'

Example

Let's increase a string length to 8 characters by padding it with '0':

let res = 'abcde'.padStart(8, '0'); console.log(res);

The code execution result:

'000abcde'

Example

Let's set the first parameter of the method to a number that is less than a length string:

let res = 'ab'.padStart('1'); console.log(res);

As a result of executing the code, the entire string will be returned:

'ab'

See also

  • the padEnd method
    that pads the current string from its end with a given string
  • the length property
    that determines a string length
azuzcsvdahy