trim() method
In addition to the trim() method, JavaScript offers two other similar functions. In addition, they used to trim the string only from one end, either left or right. The purpose of both functions is to remove whitespaces from the original string; they do not change the original string.
- trimLeft()
- trimRight()
trimLeft()
TrimLeft() removes whitespace only from the left side of the string. It removes whitespace only from the start of the string and returns it without any leading whitespace.
Syntax
str.trimLeft()
Example:
<html> <body> <script> function func_trim() { //original string with whitespace in beginning var str = " javatpoint i2Tutorials website "; //string trimmed using trimLeft() var trimmedstr = str.trimLeft(); document.write(trimmedstr); } func_trim(); </script> </html> </body>
trimRight()
In contrast, the trimRight() method removes whitespace only from the right side of the string. It removes whitespace only from the end of the string and returns it without any trailing whitespace characters.
Syntax
str.trimRight()
Example:
<html> <body> <script> function func_trim() { //original string with whitespace from the end var str = " javatpoint i2Tutorials website "; //string trimmed using trimRight() var trimmedstr = str.trimRight(); document.write(trimmedstr); } func_trim(); </script> </html> </body>
Share: