EN
JavaScript - String padStart() method example
0 points
xxxxxxxxxx
1
const text = 'A';
2
3
console.log(text.padStart(1, '*')); // A
4
console.log(text.padStart(2, '*')); // *A
5
console.log(text.padStart(3, '*')); // **A
Syntax |
|
Parameters |
|
Result | A String of the specified maxLength with fillString applied from the start. |
Description |
The method pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. The padding is applied from the start (left) of the current string. |
In this example, we pad a number
to keep only the last 3 digits.
xxxxxxxxxx
1
const number = '123456789';
2
const last3Digits = number.slice(-3);
3
const maskedNumber = last3Digits.padStart(number.length, '*');
4
5
console.log(maskedNumber);
Output:
xxxxxxxxxx
1
******789