The padEnd()
function accepts a string argument and returns that string padded by a provided padding string, until a desired final string length is reached.
padEnd(string, targetLength, paddingString)
string.padEnd(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
.
padEnd
(and its counterpart, padStart), are useful for ensuring that input strings of varying lengths conform to a single target length.
Example Formulas
"hello".padEnd(8, ".") /* Output: "hello..." */
"hello".padEnd(10, ".") /* Output: "hello....." */
"Well hello there".padEnd(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)