JavaScript String padStart()
Syntax & Examples
String.padStart() method
The padStart() method of the String class in JavaScript pads the current string with another string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the start (left) of the current string.
Syntax of String.padStart()
There are 2 variations for the syntax of String.padStart() method. They are:
padStart(targetLength)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
targetLength | required | The length of the resulting string once the current string has been padded. If this parameter is smaller than the current string's length, the current string is returned as it is. |
This method pads the current string with spaces from the start to reach the target length.
Returns value of type String
.
padStart(targetLength, padString)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
targetLength | required | The length of the resulting string once the current string has been padded. If this parameter is smaller than the current string's length, the current string is returned as it is. |
padString | optional | The string to pad the current string with. If this string is too long, it will be truncated to the required length. The default value is a space (' '). |
This method pads the current string with the specified padString from the start to reach the target length.
Returns value of type String
.
✐ Examples
1 Using padStart() method with target length only
In JavaScript, we can use the padStart()
method to pad a string with spaces from the start to reach the specified target length.
For example,
- We define a string variable
str
with the value'Hello'
. - We use the
padStart()
method with the target length10
. - The result, a string padded with spaces from the start, is stored in the variable
paddedStr
. - We log
paddedStr
to the console usingconsole.log()
method.
JavaScript Program
const str = 'Hello';
const paddedStr = str.padStart(10);
console.log(paddedStr);
Output
' Hello'
2 Using padStart() method with a target length and a pad string
In this example, we use the padStart()
method to pad a string with a specified string from the start to reach the target length.
For example,
- We define a string variable
str
with the value'Hello'
. - We use the
padStart()
method with the target length10
and the pad string'123'
. - The result, a string padded with '123' from the start, is stored in the variable
paddedStr
. - We log
paddedStr
to the console usingconsole.log()
method.
JavaScript Program
const str = 'Hello';
const paddedStr = str.padStart(10, '123');
console.log(paddedStr);
Output
'12312Hello'
3 Using padStart() method when the target length is less than the string length
In this example, we use the padStart()
method with a target length that is less than the string's length, resulting in the original string being returned.
For example,
- We define a string variable
str
with the value'Hello World'
. - We use the
padStart()
method with the target length5
. - Since the target length is less than the string's length, the original string is returned and stored in the variable
paddedStr
. - We log
paddedStr
to the console usingconsole.log()
method.
JavaScript Program
const str = 'Hello World';
const paddedStr = str.padStart(5);
console.log(paddedStr);
Output
'Hello World'
Summary
In this JavaScript tutorial, we learned about padStart() method of String: the syntax and few working examples with output and detailed explanation for each example.