The trimEnd() method of the String class in JavaScript removes whitespace from the end of a string. It is an alias for trimRight().
There are 2 variations for the syntax of String.trimEnd() method. They are:
trimEnd()
This method removes whitespace from the end of the string.
Returns value of type String
.
trimRight()
This method alias for trimEnd(). Removes whitespace from the end of the string.
Returns value of type String
.
In JavaScript, the trimEnd()
method removes whitespace from the end of a string.
For example,
str
with the value 'Hello World '
which has trailing whitespace.trimEnd()
method to remove the trailing whitespace.newStr
.newStr
to the console using the console.log()
method.const str = 'Hello World ';
const newStr = str.trimEnd();
console.log(newStr);
Hello World
In JavaScript, the trimRight()
method, an alias for trimEnd()
, removes whitespace from the end of a string.
For example,
str
with the value 'Hello World '
which has trailing whitespace.trimRight()
method to remove the trailing whitespace.newStr
.newStr
to the console using the console.log()
method.const str = 'Hello World ';
const newStr = str.trimRight();
console.log(newStr);
Hello World
In JavaScript, both trimEnd()
and trimRight()
methods perform the same function of removing whitespace from the end of a string.
For example,
str
with the value 'Hello World '
which has trailing whitespace.trimEnd()
method to remove the trailing whitespace and store the result in newStr1
.trimRight()
method to remove the trailing whitespace and store the result in newStr2
.newStr1
and newStr2
to the console using the console.log()
method.const str = 'Hello World ';
const newStr1 = str.trimEnd();
const newStr2 = str.trimRight();
console.log(newStr1); // Hello World
console.log(newStr2); // Hello World
Hello World Hello World
In this JavaScript tutorial, we learned about trimEnd() method of String: the syntax and few working examples with output and detailed explanation for each example.