The padStart()
function accepts a string argument and returns that string padded by a provided padding string, until a desired final string length is reached.
padStart(string, targetLength, paddingString)
string.padStart(targetLength, paddingString)
Code language: JavaScript (javascript)
Note that targetLength
is the length of the final output string, which includes the repeated paddingString
and the input string
.
padStart
(and its counterpart, padEnd), are useful for ensuring that input strings of varying lengths conform to a single target length.
Example Formulas
"hello".padStart(8, ".") /* Output: "...hello" */
"hello".padStart(10, ".") /* Output: ".....hello" */
"Well hello there".padStart(1, ".") /* Output: "Well hello there" (if the input string is longer than targetLength, the input string is returned alone and untruncated */
Code language: JavaScript (javascript)