JavaScript and JQuery Essentials Trainin... (32 Blogs) Become a Certified Professional

How To Implement JavaScript Trim Method?

Published on Aug 08,2019 27 Views
2 / 15 Blog from JavaScript

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

Dealing with spaces  in the strings you wish to use, can be a tedious task. JavaScript Trim Method lets you deal with this issue. In this article we will be exploring this JavaScript concept in detail.

So let us get started with with this article on JavaScript Trim, let us get familiar to the subject in hand,

JavaScript Trim

Strings are one of the basic concepts of Java programming that we use. A lot of functions are available to manipulate Strings like length(),concat(),indexOf(),charAt() etc. One such function is trim. Trim removes space from the String value and gives us the trimmed string without manipulating the original string.

Space in the sense whitespace, tab etc that are present in front and at the end of string are removed, not the ones that are in between the words of a string. The trim functionality is similar in javascript too. All browsers since IE9+,Opera 10.5+,Firefox 3.5+,Chrome 5+,Safari 5+ support this functionality.

Javascript provides 3 different types of trim functions.

So let us move on with this JavaScript Trim article and see the following function,

trimLeft() 

As the name indicates, it removes the space present to the left side of the string i.e beginning of the string or leading white spaces.

Example

var string = "Hi, Welcome to Edureka!";
console.debug(string.trimLeft());

 Output

Hi,Welcome to Edureka! 

Next in this article we have trim right function,

trimRight()

As the name indicates, it removes the space present to right side of the string i.e at the end or trailing white spaces.

Example

var string = "Hi, Welcome to Edureka!";
console.debug(string.trimRight());

Output

 Hi,Welcome to Edureka! 

So let us move on with this JavaScript Trim article and see the following function,

trim()

This removes the space present both at front and back i.e leading and trailing whitespaces.

Example

var string = "Hi, Welcome to Edureka!";
console.debug(string.trim());

 Output

Hi,Welcome to Edureka!      

Note: Trim never removes the spaces in between. It only trims leading and trailing whitespaces. The above examples demonstrates it well.

With this, we have come to the end of our  JavaScript Trim article. I hope you understood the different ways to concatenate or join strings.

Now that you know about JavaScript, check out the Web Development Certification Training by Edureka. Web Development Certification Training will help you Learn how to create impressive websites using HTML5, CSS3, Twitter Bootstrap 3, jQuery and Google APIs and deploy it to Amazon Simple Storage Service(S3). 

Got a question for us? Please mention it in the comments section of “JavaScript Trim” and we will get back to you.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.