JavaScript String padEnd()
Syntax & Examples
String.padEnd() method
The padEnd() method of the String class in JavaScript pads the current string with another string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string.
Syntax of String.padEnd()
There are 2 variations for the syntax of String.padEnd() method. They are:
padEnd(targetLength)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
targetLength | required | The length of the resulting string once the current string has been padded. If this parameter is smaller than the current string's length, the current string is returned as it is. |
This method pads the current string with spaces from the end to reach the target length.
Returns value of type String
.
padEnd(targetLength, padString)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
targetLength | required | The length of the resulting string once the current string has been padded. If this parameter is smaller than the current string's length, the current string is returned as it is. |
padString | optional | The string to pad the current string with. If this string is too long, it will be truncated to the required length. The default value is a space (' '). |
This method pads the current string with the specified padString from the end to reach the target length.
Returns value of type String
.
✐ Examples
1 Using padEnd() method with target length only
In JavaScript, we can use the padEnd()
method to pad a string with spaces from the end to reach the specified target length.
For example,
- We define a string variable
str
with the value'Hello'
. - We use the
padEnd()
method with the target length10
. - The result, a string padded with spaces from the end, is stored in the variable
paddedStr
. - We log
paddedStr
to the console usingconsole.log()
method.
JavaScript Program
const str = 'Hello';
const paddedStr = str.padEnd(10);
console.log(paddedStr);
Output
'Hello '
2 Using padEnd() method with a target length and a pad string
In this example, we use the padEnd()
method to pad a string with a specified string from the end to reach the target length.
For example,
- We define a string variable
str
with the value'Hello'
. - We use the
padEnd()
method with the target length10
and the pad string'123'
. - The result, a string padded with '123' from the end, is stored in the variable
paddedStr
. - We log
paddedStr
to the console usingconsole.log()
method.
JavaScript Program
const str = 'Hello';
const paddedStr = str.padEnd(10, '123');
console.log(paddedStr);
Output
'Hello12312'
3 Using padEnd() method when the target length is less than the string length
In this example, we use the padEnd()
method with a target length that is less than the string's length, resulting in the original string being returned.
For example,
- We define a string variable
str
with the value'Hello World'
. - We use the
padEnd()
method with the target length5
. - Since the target length is less than the string's length, the original string is returned and stored in the variable
paddedStr
. - We log
paddedStr
to the console usingconsole.log()
method.
JavaScript Program
const str = 'Hello World';
const paddedStr = str.padEnd(5);
console.log(paddedStr);
Output
'Hello World'
Summary
In this JavaScript tutorial, we learned about padEnd() method of String: the syntax and few working examples with output and detailed explanation for each example.