JavaScript padEnd
Method
Introduction
This tutorial covers the usage of the padEnd
method in JavaScript. The padEnd
method is used to pad a string with a specified character or a sequence of characters until the resulting string reaches a desired length.
Syntax
The syntax for the padEnd
method is as follows:
string.padEnd(targetLength [, padString]);
targetLength
: The length of the resulting string.padString
(optional): The string to pad the current string with. If not provided, the string is padded with spaces.
Example
Consider a scenario where you want to pad a string to a specific length:
const originalString = 'Hello';
const paddedString = originalString.padEnd(10, '-');
console.log(paddedString); // Output: 'Hello-----'
Output
The output of the example is a string padded with hyphens to reach the target length:
'Hello-----'