Remove whitespace from strings:
let input = " hello "
console.log(input.trim()) // "hello"
console.log(input.trimStart()) // "hello "
console.log(input.trimEnd()) // " hello"
This is useful for cleaning user input:
let username = userInput.trim()
if (username.length > 0) {
// Process valid input
}
Whitespace includes spaces, tabs, and newlines.