String padEnd()
In JavaScript, String.padEnd()
method is used to pad the ending of the string such that the resulting string is of specified length.
Syntax
str.padEnd(n, pad)
where
str
is a string.n
is an integer.pad
is a string.
padEnd()
method makes a copy of the string str
, pads given pad
string to the end of calling string str
such that the length of resulting string is n
, and returns the resulting string. Original string str
is not modified. If n
is less than length of original str
, then padEnd() returns original string str
as is.
Examples
1. In the following program, we take a string in name
, and pad this string at end such that the length of resulting string is 10.
let name = 'apple';
let output = name.padEnd(10, '#');
console.log(output);
2. In the following program, we check how padded() method behave when the desired string length n
is less than length of original string.
let name = 'apple';
let output = name.padEnd(4, '#');
console.log(output);