The padEnd() method in JavaScript is used to pad the current string with another string until the resulting string reaches the given length. The padding is applied from the end (right side) of the string.
Syntax: string.padEnd(targetLength, padString)
- targetLength: The length of the resulting string after the padding is applied. If the value is less than or equal to the length of the original string, the original string is returned unchanged.
- padString (optional): The string to pad the current string with. If not specified, the current string is padded with spaces.
Example:
let str = "Hello"; let paddedStr = str.padEnd(10, "12345"); console.log(paddedStr); // Output: "Hello12345"
Explanation:
- We have a string str containing "Hello".
- We use padEnd() to pad the string with "12345" until its length reaches 10 characters.
- The resulting string, "Hello12345", is stored in the variable paddedStr and logged to the console.
Code Compiler Link: https://app.singhteekam.in/codecompiler/