In brief
trim() returns new text without the whitespace at the beginning and end. Internal intervals remain.
Useful to know
trim() removes whitespace from both ends of a text string and returns a new string. The original value does not change. The method is convenient before checking whether the user has actually written something in a form. [1]
Example: const query = ' red tea '; const cleaned = query.trim();. cleaned is 'red tea', but query still contains the surrounding spaces. So assign or use the returned result instead of just calling the method.
The internal interval between "red" and "tea" remains . trim() is not a function to remove all spaces and does not correct spelling. For a string of spaces only, the result is the empty string ''. This allows a clear check: cleaned === ''.
Do not present it as a defense against dangerous HTML or SQL. It normalizes the edges of the text; validation, safe display, and database access are other tasks.
Sources
For further detail and to check the information: