trim() / trimStart() / trimEnd()
method
ES2019
Removes whitespace from both ends, just the beginning, or just the end of a string.
Syntax
string.trim() string.trimStart() string.trimEnd()
Example
javascript
const raw = ' hello world ';
console.log(raw.trim()); // 'hello world'
console.log(raw.trimStart()); // 'hello world '
console.log(raw.trimEnd()); // ' hello world'
// Clean user input
const userInput = ' john@example.com ';
const email = userInput.trim().toLowerCase();
console.log(email); // 'john@example.com'