JavaScript remove spaces from beginning and end of string with trim

Опубликовано: 21 Май 2026
на канале: Profu' de geogra'
9
0

The trim() method in JavaScript is used to remove whitespace from the beginning and end of a string. It doesn't affect spaces or characters that are inside the string (between words). This is especially useful when you want to clean up user input, like when they accidentally add spaces at the start or end of a text. Syntax is string.trim()
Key Points:
Removes leading and trailing spaces.
Does not modify spaces inside the string.
Returns a new string without the extra spaces (it does not modify the original string, since strings in JavaScript are immutable).
Does not remove non-breaking spaces ( ) or other special Unicode spaces; however, it does remove regular spaces, tabs, and line breaks.