- JavaScript String trim()
- Description
- See Also:
- Syntax
- Parameters
- Return Value
- Related Pages
- Browser Support
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- String.prototype.trim()
- Try it
- Syntax
- Return value
- Examples
- Using trim()
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- JavaScript: String trim() method
- Description
- Syntax
- Parameters or Arguments
- Returns
- Note
- Example
- How to Left Trim and Right Trim String in JavaScript
- How to Left Trim and Right Trim String in JavaScript?
- How to Use trim() Method in JavaScript?
- How to Use trimLeft() Method in JavaScript?
- How to Use trimRight() Method in JavaScript?
- Conclusion
- About the author
- Farah Batool
- How to Trim Strings in JavaScript
JavaScript String trim()
Remove spaces with replace() using a regular expression:
Description
The trim() method removes whitespace from both sides of a string.
The trim() method does not change the original string.
See Also:
Syntax
Parameters
Return Value
Related Pages
Browser Support
trim() is an ECMAScript5 (ES5) feature.
ES5 (JavaScript 2009) fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
String.prototype.trim()
The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string.
To return a new string with whitespace trimmed from just one end, use trimStart() or trimEnd() .
Try it
Syntax
Return value
A new string representing str stripped of whitespace from both its beginning and end. Whitespace is defined as white space characters plus line terminators.
If neither the beginning or end of str has any whitespace, a new string is still returned (essentially a copy of str ).
Examples
Using trim()
The following example trims whitespace from both ends of str .
const str = " foo "; console.log(str.trim()); // 'foo'
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Mar 26, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
JavaScript: String trim() method
This JavaScript tutorial explains how to use the string method called trim() with syntax and examples.
Description
In JavaScript, trim() is a string method that is used to remove whitespace characters from the start and end of a string. Whitespace characters include spaces, tabs, etc. Because the trim() method is a method of the String object, it must be invoked through a particular instance of the String class.
Syntax
In JavaScript, the syntax for the trim() method is:
Parameters or Arguments
There are no parameters or arguments for the trim() method.
Returns
The trim() method returns a string with whitespace characters removed from the start and end of the string.
Note
Example
Let’s take a look at an example of how to use the trim() method in JavaScript.
var totn_string = ' TechOnTheNet '; console.log(totn_string.trim());
In this example, we have declared a variable called totn_string that is assigned the string value of ‘TechOnTheNet’. We have then invoked the trim() method of the totn_string variable to remove the whitespace characters from the start and end of the string.
We have written the output of the trim() method to the web browser console log, for demonstration purposes, to show what the trim() method returns.
The following will be output to the web browser console log:
In this example, the trim() method removed the whitespace characters from the start and end of the string ‘ TechOnTheNet ‘ and returned a string value of ‘TechOnTheNet’.
How to Left Trim and Right Trim String in JavaScript
Like other programming languages, in JavaScript, strings are an important form of variable, and the developers frequently need to edit or modify strings to fulfill their requirements, such as removing extra white spaces from a string, including “tab”, “space”, “line terminating characters” from either beginning or the end or on both sides of the string.
This article will explain the method of trimming the string from the right or the left side in JavaScript.
How to Left Trim and Right Trim String in JavaScript?
For left or right string trimming, JavaScript offers some built-in methods, including:
Let’s check out each of them one by one!
How to Use trim() Method in JavaScript?
The “trim()” method does not modify the original string, it only removes the whitespace characters from both sides, the starting, and end of a string.
Follow the given syntax for using the trim() method to trim the strings:
The trim() method calls along the string that will be trimmed and returns a new string by eliminating extra white spaces from the specified string.
First, we will create a “string” that contains extra white spaces at the start and the end of the string:
Then, call the trim() method and store the resultant string in the variable “answer”:
Lastly, print the resultant string on the console using the “console.log()” method:
Now, we use the “length” property that returns the length of the string before and after the trimming:
console.log ( «Length of the Actual String is » + string.length ) ;
console.log ( «Length of the Resultant String is » + answer.length ) ;
As you can see in the output, the length of the actual string is “33” which contains spaces, and the resultant string’s length is “24”. This states that the white spaces from the start and the end of the string are successfully trimmed:
If you want to remove the whitespaces only from the start of the string, follow the below section.
How to Use trimLeft() Method in JavaScript?
The “trimLeft()” eliminates the leading white spaces in a string. It works similarly to the “trimStart()” method. Both methods act as the same because the trimStart() is the alias of the trimLeft() method.
Use the following syntax for trimming the string from the left or the start of the string:
We will first create a string with three whitespaces at the beginning of the string and the same at the end of the string:
Now, call the trimLeft() method to trim the spaces from the left side or start of the string:
Finally, print the string on the console:
Check the length of the string before and after the trimming using the “length” property of the string:
console.log ( «Length of the Actual String is » + string.length ) ;
console.log ( «Length of the Resultant String is » + answer.length ) ;
The output shows that the “trimLeft()” method successfully trimmed the white spaces present at the beginning of the string:
You can also utilize the “trimStart()” method instead of the trimLeft() method for the same purpose:
It outputs the same result as the trimLeft() method:
Want to know the method to specifically remove extra spaces from the right side of the string? Follow the given-provided method.
How to Use trimRight() Method in JavaScript?
To trim the string from the right side of the string, use the JavaScript predefined “trimRight()” which is also known as the “trimEnd()” method. It is primarily utilized for removing spaces from the end of the string or from the string’s right side.
The syntax for the trimRight() method is as follows:
We will now utilize the same string and eliminate spaces from the right side of it by calling the “trimRight()” method:
It can be seen from the output that extra whitespaces from the end of the string are removed:
Now, use the “trimEnd()” method instead of the trimRight() method for the same scenario:
We have covered all the essential instructions related to the left and right trimming process of the string in JavaScript.
Conclusion
To the left and right trim string in JavaScript, use the JavaScript predefined methods including the “trim()” method, “trimLeft()” or “trimStart()” method, and the “trimRight()” or “trimEnd()” method. The trim() method trims the strings from the both left and the right of the string, the trimLeft() or the trimStart() method trims the string from the start, while the trimRight() or the trimEnd() method trims the string from the end. In this article, we have explained the procedure to trim the string from the right or the left side with detailed examples.
About the author
Farah Batool
I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.
How to Trim Strings in JavaScript
A good practice when using string values from form fields is to remove the whitespaces from the start and end of the strings — i.e. trim the string.
In this post, I’m going to describe what is a whitespace and a line terminator character in JavaScript.
Plus, you’ll read how to trim strings, aka remove whitespaces and line terminator characters from the start and/or end of the string.
Before I go on, let me recommend something to you.
The path to becoming good at JavaScript isn’t easy. but fortunately with a good teacher you can shortcut.
Take «Modern JavaScript From The Beginning 2.0» course by Brad Traversy to become proficient in JavaScript in just a few weeks. Use the coupon code DMITRI and get your 20% discount!
1. The whitespaces and line terminators
Before diving into the actual trim functions, let’s first agree what special characters the trim functions are removing from strings.
First, the whitespace is any character from the following list:
- SPACE ( U+0020 code point)
- CHARACTER TABULATION ( U+0009 code point)
- LINE TABULATION ( U+000BU code point)
- FORM FEED (FF) ( U+000C code point)
- NO-BREAK SPACE ( U+00A0 code point)
- ZERO WIDTH NO-BREAK SPACE ( U+FEFFU code point)
- Any other character from Space Separator category
In simple words, the whitespaces are characters that rendered on the screen create an empty white space.
Common whitespace characters are space ‘ ‘ and tab ‘\t’ .
Secondly, the line terminator is also a special set of characters consisting of:
- LINE FEED ( U+000A code point)
- CARRIAGE RETURN ( U+000D code point)
- LINE SEPARATOR ( U+2028 code point)
- PARAGRAPH SEPARATOR ( U+2029 code point)
The line terminator represents a character that exists at the end of a text line and has some special purpose.
A common line terminator character is the line feed ‘\n’ , which means moving one line forward.
2. Trim strings in JavaScript
There are situations when you want to clean strings entering from the application input. For example, you’d definitely want to trim strings from the form fields representing a username, first name, last name, phone number, etc.
JavaScript provides 3 simple functions on how to trim strings.
string.trim() removes sequences of whitespaces and line terminators from both the start and the end of the string.