When you're working with strings, you usually don't want to have whitespace at the start or the end of a string.
This can happen when you're working with strings that you don't control and can therefore have characters you don't want.
In this post, we'll learn how to remove whitespace at the start and end of a string in JavaScript.
Using the trim() method
The easiest way to trim whitespace from the start and end of a string is to use the trim() method. This method is called directly on the string variable and returns the string with the whitespace removed.
When you're only interested in removing the whitespace from the beginning of the string, use the trimStart() method. It functions exactly like the trim method except
Alternatively, when you only want to remove whitespace from the end of the string, use the trimEnd() method. It's the opposite of the trimStart() method.