The padStart()
method appends string to the start to make it certain length.
The first argument is the desired length, and the second is the optional string to add as a pad.
If not provided, the U+0020 'space' character will be used.
str.padStart(targetLength [, padString])
targetLength
- the length of the resulting stringpadString
- Optional, the string to padconsole.log('abc'.padStart(10)); // " ? ? ? ? ? ? abc" console.log('abc'.padStart(10, "foo")); // "foofoofabc" console.log('abc'.padStart(6,"123465")); // "123abc" console.log('abc'.padStart(8, "0")); // "00000abc" console.log('abc'.padStart(1)); // "abc"
let stringValue = "foo"; console.log(stringValue.padStart(6)); // " foo" console.log(stringValue.padStart(9, ".")); // "......foo"
The optional argument is not limited to a single character.
let stringValue = "foo"; console.log(stringValue.padStart(8, "bar")); // "barbafoo" console.log(stringValue.padStart(2)); // "foo"