Use .trim() to remove leading and trailing whitespace
const text = ' Hello World ';
const trimmed = text.trim();
console.log(trimmed); // "Hello World"const userInput = ' [email protected] ';
// Clean user input
const email = userInput.trim().toLowerCase();
console.log(email);
// Remove all whitespace
const phone = ' (555) 123-4567 ';
const cleaned = phone.replace(/\s/g, '');
console.log(cleaned);const text = ' hello ';
text.trimStart(); // "hello "
text.trimEnd(); // " hello"const text = 'too many spaces';
const normalized = text.replace(/\s+/g, ' ');
// "too many spaces"Avoid manual string manipulation
// DON'T DO THIS
var trimmed = text.replace(/^\s+|\s+$/g, '');✓ Works in all modern browsers (ES5+)