The string is a simple text or characters which can include the alphabets, numbers, or symbols.
Syntax for the trim() method is as follows:
Let’s try some examples and understand it.
First, we suppose a string and add some extra white space around the string.
Now, to get rid of the extra white spaces from both sides, we try to apply the trim() method over that string and see how it works.
We can see in the output that the string is trimmed and there is no extra whitespace left around the string as we desired it to happen.
Now, a question arises: what if we want to trim the string from only the left side or the start of the string and vice versa. There is a built-in function for that as well. There are two different trimStart() and trimLeft() functions, but these both do the same task. So, if we want to trim the string from the left side only and want to keep the whitespaces on the right side. We can use the trimStart() or trimtrimLeft() function.
As you can see that both functions do the same task and trim the strings from the left side only.
Similarly, if we want to trim the string from the last or the right side only. We can use any of the trimEnd() or trimRight() functions.
It is observed that the string is trimmed only from the right side, as we expected.