EN
JavaScript - String padStart() method example
0
points
// ONLINE-RUNNER:browser;
const text = 'A';
console.log(text.padStart(1, '*')); // A
console.log(text.padStart(2, '*')); // *A
console.log(text.padStart(3, '*')); // **A
1. Documentation
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. |
2. Practical example
In this example, we pad a number
to keep only the last 3 digits.
// ONLINE-RUNNER:browser;
const number = '123456789';
const last3Digits = number.slice(-3);
const maskedNumber = last3Digits.padStart(number.length, '*');
console.log(maskedNumber);
Output:
******789