Javascript String padEnd()

The JavaScript String padEnd() method pads the current string with another string to the end.

The syntax of the padEnd() method is:

str.padEnd(targetLength, padString)

Here, str is a string.

padEnd() Parameters

The padEnd() method takes in:

  • targetLength - The length of the final string after current string has been padded. For targetLength < str.length, the string is returned unmodified.
  • padString (optional) - The string to pad current string with. Its default value is " ".

Note: If padString is too long, it will be truncated to meet targetLength.

Return value from padEnd()

  • Returns a String of the specified targetLength with padString applied to end of the current string.

Example: Using padEnd()

let string = "CODE";

value1 = string.padEnd(10);
console.log(value1); // "CODE      "

value2 = string.padEnd(10, "*");
console.log(value2); // "CODE******"

// long string is truncated
value3 = string.padEnd(10, "ABCDEFGHIJKL");
console.log(value3); // "CODEABCDEF"



Recommended Reading: JavaScript String padStart()