This content originally appeared on Go Make Things and was authored by Go Make Things
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();
This content originally appeared on Go Make Things and was authored by Go Make Things
Go Make Things | Sciencx (2021-06-10T14:30:00+00:00) How to trim whitespace from the beginning and end of a string with vanilla JS. Retrieved from https://www.scien.cx/2021/06/10/how-to-trim-whitespace-from-the-beginning-and-end-of-a-string-with-vanilla-js/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.