JavaScript remove whitespace from string; Through this tutorial, i am going to show you how to remove whitespace from a string using trim()
method.
JavaScript Remove Whitespace from a String
You can use the following javaScript trim() method to remove whitespace from beginning and end of string.
Syntax of JavaScript trim() method
See the following syntax of javaScript trim() method; as shown below:
let res = str.trim();
Here,
- The
trim()
method doesn’t change the original string.
Example 1 – JavaScript remove whitespace from beginning and end of string
See the following example for remove whitespace from beginning and end of string using javaScript trim()
method; as shown below:
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.
Be First to Comment