Java/J2EE and SOA (342 Blogs) Become a Certified Professional
AWS Global Infrastructure

Front End Web Development

Topics Covered
  • AngularJS (35 Blogs)
  • jQuery UI Development (1 Blogs)
  • The Complete WebDeveloper (43 Blogs)
  • ReactJS (7 Blogs)
SEE MORE

How To Implement JavaScript Trim Method?

Last updated on Apr 14,2022 966 Views

11 / 31 Blog from JavaScript

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. To learn more about Javascript, check out this web developer course today.

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.

Upcoming Batches For Java Certification Training Course
Course NameDate
Java Certification Training Course

Class Starts on 29th October,2022

29th October

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 26th November,2022

26th November

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Implement JavaScript Trim Method?

edureka.co