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.
More JavaScript Tutorials
Recommended:-Guide to JavaScript Strings By Examples
Recommended:-Javascript String ToLowerCase
Recommended:-JavaScript Convert String to UpperCase
Recommended:-JavaScript Concatenate Strings
Recommended:-JavaScript Compare String Examples
Recommended:-JavaScript Convert String to Array
Recommended:-JavaScript Replace String Method
Recommended:-Check if String Contains Substring JavaScript
Recommended:-JavaScript Comparison Operators Examples
Recommended:-JavaScript Ternary Operators Examples
Recommended:-JavaScript Switch Case Statement Tutorial
Recommended:-JavaScript While, Do-While, For and For-In Loops
Recommended:-JavaScript While Loop with Example
Recommended:-JavaScript do-while Loop with Example
Recommended:-JavaScript for Loop with Examples
Recommended:-Continue Statement in JavaScript
Recommended:-JavaScript Error Try-Catch Statement
Recommended:-Break Statement in JavaScript