How to Remove Whitespace from String in Javascript?

Overview
You may have noticed that if you search something on the internet with whitespaces at the front or rear ends, the search engine implicitly removes the whitespaces at the ends.
In JavaScript, we can remove the leading and trailing whitespaces using the trim() method.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Introduction
In a string, space separates the words to make it a meaningful sentence, and whitespace is just unnecessary space. It can be in front, back, or both in a string.
Whenever we have to make sure that our string doesn't contain any whitespaces in front or rear part, we used to trim string in javascript to remove leading and trailing whitespaces.
To trim a string in javascript we have to invoke the trim() method on the string as shown below. It returns a new string with no whitespaces.
An example of using trim() method:
Output:
Explanation:
- Above, we have a variable myStr and we have applied the trim() method on it.
- The trim method returns the trimmed string "This is my string" after removing leading and trailing whitespaces.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Syntax of trim in Javascript
Syntax of the trim method-
Return value of trim in Javascript
The trim() method looks for whitespace in the front and rear part of string and then returns the new trimmed string after removing leading and trailing whitespaces. Let's understand it by an example:
Output:
Explanation:
- Above, we have a variable testStr holding a string value and we are calling the trim() method on it, as result, trim() method looks for whitespaces in the front and the rear part of the string and returns the new trimmed string.
- The Time and Space complexities of the trim() method is O(N) where N is the size of the string. It is because in the worst case, the whole string can contain only whitespaces leading to traversal of the whole string.
- Next, we assigned the value returned by trim() method in returnVal variable then log returnVal on console, as result, we print the trimmed string on the console. This verifies, calling trim method on string having whitespace, returns the new trimmed string after removing leading and trailing whitespaces.
Now, that we know how the trim method works, let's take some more examples to understand the different scenarios of using it.
Examples of trim in Javascript
1. To remove the whitespace from the beginning of the string:
Output:
2. To remove the whitespace from the end of the string-
Output:
3. To remove the whitespace from both the beginning and end-
Output:
Browser compatibility
trim is a method to trim string in javascript, it is a feature of ES5(ECMAScript 5) which is supported in all major browsers.
| Browser: | Chrome | Firefox | Mozila | Edge | Opera |
|---|---|---|---|---|---|
| Support | Yes | Yes | Yes | Yes | Yes |
Conclusion
- The trim() is a predefined method in javascript to remove leading and trailing whitespaces and return a new string without whitespaces.
- The trim() method return a new string, without modifying the original string.
- The trim method works only with the string value.
- Time and space complexity of trim method is O(N) where N is the size of the string.