How to trim whitespace from the beginning and end of a string with vanilla JS
Let’s imagine you have a string with some extra spaces at the beginning and end of it.
let str = ' I love Cape Cod potato chips. ';
How would you remove that unneeded whitespace? JavaScript provides three different methods, depending on what you’re trying to do.
The String.trim()
method removes leading and trailing whitespace from a string.
// Returns "I love Cape Cod potato chips."
str.trim();
If you only want to remove the leading whitespace, you can instead use the String.trimStart()
method.
// Returns "I love Cape Cod potato chips. "
str.trimStart();
And if you only want to remove the trailing whitespace, you can use the String.trimEnd()
method.
// Returns " I love Cape Cod potato chips."
str.trimEnd();