Whenever you use trim() on a string, the method does the following:
- It trims whitespace from both ends of the string.
- It returns the new version of the calling string—without changing the original string.
Syntax of the trim()
Method
trim()
accepts no arguments. Here is the syntax:
Example: Use trim()
to Remove Whitespace from Both Ends of a String
Try it on CodePen
Note that you can alternatively use replace()
and regular expression to implement trim()
’s functionality.
Example: Use replace()
and Regular Expression to Remove Whitespace from Both Ends of a String
Try it on CodePen