Latest Code Tutorials

Javascript Trim Example | String.prototype.trim() Tutorial


Javascript Trim Example | String.prototype.trim() Tutorial is today’s leading topic. String trimming is one of the most common tasks in any programming language that programmers to deal with.  The trim() method removes whitespace from both sides of the string. Whitespace in this context is all of the whitespace characters including space, tab, no-break space, etc., and all the line terminator characters including LFCR, etc. The trim() method returns the string stripped of whitespaces from both ends of the string. The trim() does not affect the value of the String itself. The trim() function does not take any arguments.

If you want to learn more about the latest Javascript tutorials then check out this The Complete Javascript Course 2018.

Javascript Trim Example | String.prototype.trim() Tutorial

The syntax for the function is the following.


The trim() does not affect the value of the string itself. JavaScript provides three functions for performing the various types of string trimming. The first, trimLeft() that will strip the characters from the beginning of the string. The second, trimRight() that will remove the characters from an end of the string. The final function is trim() that removes characters from both ends.

#trimLeft() method

Let us take the following code to understand the trimLeft() method.

// server.js

let string = "  AppDividend";

Here, we have given the spaces from the start of the string. Means, the left side of the string there is a space. Now in the output, the spaces will be removed. We can enhance trimLeft() function by specifying the list of characters to remove. The enhanced version is shown below.

String.prototype.trimLeft = function(charlist) {
  if (charlist === undefined)
    charlist = "\s";

  return this.replace(new RegExp("^[" + charlist + "]+"), "");

#trimRight() method

If the string’s right side is space, then it will remove it.

// server.js

let string = "AppDividend       ";

#trim() method

The last function is elementary, and it relies on the previous two methods that we have discussed earlier. The javascript trim() method trims both at the beginning and the end of the string. The trim() function is useful for removing extra spaces typed by the users. Often, the users aren’t even aware that they have typed extra spaces. This fact could also lead to the login problems if, for example, the user registered himself with the trailing whitespace.

// server.js

let string = "     AppDividend       ";

In the output, the space from both sides is trimmed.

The trim from jQuery is convenient if you are already using that library.

jQuery.trim('  your string   ');

For browsers that do not support the string.trim(), e.g., Internet Explorer 8 or earlier, you can add your own trim() method to string object’s prototype by including a following script in the <HEAD> section of your page, or if you are old node version then you need to add that script file at the start of your project prior to any script that uses string.trim():

Earn a Tech Degree and get the skills like Frontend Development or Javascript Development that can help you to launch a career. Start your free trial
if (!String.prototype.trim) {
 String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g,'');

Finally, Javascript Trim Example | String.prototype.trim() Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.