All TalkersCode Topics

Follow TalkersCode On Social Media

JavaScript Trim Character

Last Updated : Jan 1, 2023

JavaScript Trim Character

In this tutorial we will show you the solution of JavaScript trim character, trim in JavaScript is used to remove the whitespaces from a string in JavaScript.

This function helps us to remove whitespace from the front and also from the end of the given string, in addition, we can also say that trim helps to remove additional whitespace from a string.

Let us understand how to use trim in JavaScript.

Step By Step Guide On JavaScript Trim Character :-

As of now, we understand what the basic use of trim is and for what purpose trim is used.

Now, we are going to understand how to use trim in JavaScript.

Is there one single method in trim to remove leading and trailing whitespace or two different functions which help us to remove whitespace from both sides differently?

But before them let us see a basic example of trim and how to use trim in JavaScript.

var string = " Hello World! ";
// prints "Hello World!"

This is the basic example of a trim function that how uses trim in JavaScript.

As we see that this function removes extra whitespace from both sides that are left and right.

But what if we want to remove whitespace space from only one side then, what function will help us to remove extra whitespace? Let see:

// trim left function
var string = " Hello world";
console.log(string.trimLeft(" "));
// prints "Hello world"
// trim right function
var string = "Hello world ";
console.debug(string.trimRight(" "));
// prints "Hello world"
  1. Here, above we show you an example in JavaScript.
  2. Here, as above we see three examples. At first, we just see how to use the trim function in JavaScript. And in the next two, we see how to use trim left and trim right functions to remove whitespace from specific side.
  3. One thing to note here is that the trim function is not only used to remove whitespace. This function also helps to remove a specific character from strings.
  4. In this tutorial, we only show you how to use the trim function to remove whitespace from a string, because this is the basic way to use the trim function.
  5. We will also show you how to trim characters from a given string using trim in JavaScript in the next tutorial, so if you want to see how to trim characters from a string please go through from next tutorial.
  6. Now, in our above examples we see that we use the trim function on a required string to remove whitespace from that string.
  7. Whereas we use the trim left and trim right function on other required strings to remove whitespace from the left side and the right side respectively.
  8. For a better understanding, you can see the above example that how we use these functions in JavaScript.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we can understand how to trim characters in JavaScript.

I hope this article on JavaScript trim character helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials