The String.trimEnd()
method is used to remove white spaces from the end of a string in JavaScript. White space characters include spaces, tabs, the carriage return, etc.
trimEnd()
must be called from an instance of a string
since it is a method of the string
object.
It does not change the value of the original string.
Note:
trimRight()
is an alias of thetrimEnd()
method and removes trailing white space characters from the right side of the string.
string.trimEnd();
string.trimEnd()
returns a new string with the trailing white spaces removed.
var example_string = " This is the example string \t";console.log(`With trailing white-spaces: "`+example_string+ `"`);console.log(`Without trailing white-spaces: "`+example_string.trimEnd()+ `"`);
The code above shows how the trimEnd()
method is used. As we can see, the string without trailing white spaces does not contain any white spaces to its right, while the white spaces to the left are preserved.
Free Resources