let text = " Hello World! "; // output : Hello World!
let result = text.trim();
//Remove spaces with replace() using a regular expression:
let text = " Hello World! ";
let result = text.replace(/^s+|s+$/gm,''); // output : Hello World!
trim is a method that removeds whitespaces from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.)
const arr = [' a ', ' b ', ' c '];
const results = arr.map(element => {
return element.trim();
});
console.log(results);
//result
//['a', 'b', 'c']
//trim single string
var str=" abc c a "
str.trim()
console.log(str);
//result
//abc c a
/**
* `" some string with spaces on both ends ".trim()`
* removes whitespace from both ends of a string
* @returns a new string, without modifying the original string
*/