EN
JavaScript - trim whitespace from string
0 points
In this article, we would like to show you how to trim whitespace from a string in JavaScript.
Quick solution:
xxxxxxxxxx
1
let text = ' abc ';
2
3
let result = text.trim();
4
5
console.log(result);
In this example, we use trim()
method that removes whitespace from both sides of a string.
xxxxxxxxxx
1
let text1 = ' abc';
2
let text2 = 'abc ';
3
let text3 = ' abc ';
4
5
let result1 = text1.trim();
6
let result2 = text2.trim();
7
let result3 = text3.trim();
8
9
console.log(result1); // abc
10
console.log(result2); // abc
11
console.log(result3); // abc