.padEnd()

Published Jun 8, 2024
Contribute to Docs

In JavaScript, the .padEnd() method is used with JavaScript strings to pad the end of a string with another string, until it reaches a specified length. It was introduced with ECMAScript 2017.

Syntax

string.padEnd(targetLength, padString)
  • string: The original string to be padded.
  • targetLength: The desired length of the resulting string after padding.
  • padString: The string to pad the original string with, defaulting to a space if not provided.

Example

In the following example, a string variable called text contains the sentence ‘hello world’. The .padEnd() method is used to append more characters (in this example exclamation marks) to the end of the text, until it reaches the specified length of 15 characters.

let text = 'hello world';
console.log(text.padEnd(15, '!'));

This results in the following output:

'hello world!!!!'

Codebyte Example

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn JavaScript on Codecademy