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. ';
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
// Returns "I love Cape Cod potato chips. " str.trimStart();
And if you only want to remove the trailing whitespace, you can use the
// Returns " I love Cape Cod potato chips." str.trimEnd();