How To Implement JavaScript Trim Method?

Last updated on Dec 20,2023 1.7K Views

How To Implement JavaScript Trim Method?

edureka.co

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). 

If you want to get trained in React and wish to develop interesting UI’s on your own, then check out the React JS training online by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

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 NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

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

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial