javascript remove whitespace from string; In this tutorial, you’ll learn how to javascript remove whitespace from beginning and end of string using JavaScript trim()
method.
JavaScript trim() method
The javascript String.trim()
is a string method that is used to remove whitespace characters from the beginning and from the end of a string.
let res = str.trim();
Here,
- The
trim()
method doesn’t change the original string.
if you want to remove only starting whitespace or ending whitespace from the given string, You can use javascript trimStart()
or trimEnd()
methods. See the followings:
In JavaScript, trimStart()
is a string method that is used to remove whitespace characters from the start of a string.
In JavaScript, trimEnd()
is a string method that is used to remove whitespace characters from the end of a string.
Note that, The whitespace characters include space, tab, no-break space, etc.
Examples: JavaScript trim()
Let’s take a took the following example:
Use the trim()
to remove whitespace from both sides of a given string, see the following example:
let str = ' javascript programming '; let result = str.trim(); console.log(result);
Output:
"javascript programming"
Conclusion
In this tutorial, you have learned how to remove whitespace characters from both ends of a string using the js trim()
method.