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.


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();

Here’s a demo.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to trim whitespace from the beginning and end of a string with vanilla JS." Go Make Things | Sciencx - Thursday June 10, 2021, https://www.scien.cx/2021/06/10/how-to-trim-whitespace-from-the-beginning-and-end-of-a-string-with-vanilla-js/
HARVARD
Go Make Things | Sciencx Thursday June 10, 2021 » How to trim whitespace from the beginning and end of a string with vanilla JS., viewed ,<https://www.scien.cx/2021/06/10/how-to-trim-whitespace-from-the-beginning-and-end-of-a-string-with-vanilla-js/>
VANCOUVER
Go Make Things | Sciencx - » How to trim whitespace from the beginning and end of a string with vanilla JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/10/how-to-trim-whitespace-from-the-beginning-and-end-of-a-string-with-vanilla-js/
CHICAGO
" » How to trim whitespace from the beginning and end of a string with vanilla JS." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2021/06/10/how-to-trim-whitespace-from-the-beginning-and-end-of-a-string-with-vanilla-js/
IEEE
" » How to trim whitespace from the beginning and end of a string with vanilla JS." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2021/06/10/how-to-trim-whitespace-from-the-beginning-and-end-of-a-string-with-vanilla-js/. [Accessed: ]
rf:citation
» How to trim whitespace from the beginning and end of a string with vanilla JS | Go Make Things | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.