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:
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