Skip to content

JavaScript padEnd() method

  • by

The padEnd() method in JavaScript is a built-in function that can be used to pad a string with a specified character or set of characters at the end of the string until the string reaches a specified length.

padEnd(targetLength)
padEnd(targetLength, padString)

JavaScript padEnd() method example

Simple examples of using the padEnd() method in JavaScript:

Padding a string with spaces

const str = 'Hello';
console.log(str.padEnd(10));

Output:

JavaScript padEnd() method

Padding a string with a custom character

const str = 'Hello';
console.log(str.padEnd(10, '!'));

Output: Hello!!!!!

Handling strings longer than the target length

const str = 'This string is already longer than 10 characters';
console.log(str.padEnd(10));

Output: This string is already longer than 10 characters

Padding a number with zeros

const num = 42;
console.log(num.toString().padEnd(5, '0'));

Output: 42000

This method is useful for formatting strings and is available in modern browsers and Node.js versions 8.0.0 and later.

Do comment if you have any doubts or suggestions on this Js Method.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading