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:
// ONLINE-RUNNER:browser;
let text = ' abc ';
let result = text.trim();
console.log(result);
Practical example
In this example, we use trim()
method that removes whitespace from both sides of a string.
// ONLINE-RUNNER:browser;
let text1 = ' abc';
let text2 = 'abc ';
let text3 = ' abc ';
let result1 = text1.trim();
let result2 = text2.trim();
let result3 = text3.trim();
console.log(result1); // abc
console.log(result2); // abc
console.log(result3); // abc