totn JavaScript

JavaScript: String trimEnd() method

This JavaScript tutorial explains how to use the string method called trimEnd() with syntax and examples.

Description

In JavaScript, trimEnd() is a string method that is used to remove whitespace characters from the end of a string. Whitespace characters include spaces, tabs, etc. Because the trimEnd() method is a method of the String object, it must be invoked through a particular instance of the String class.

Syntax

In JavaScript, the syntax for the trimEnd() method is:

string.trimEnd();

Parameters or Arguments

There are no parameters or arguments for the trimEnd() method.

Returns

The trimEnd() method returns a string with whitespace characters removed from the end of the string.

Note

  • The trimEnd() method does not change the value of the original string.
  • The trimEnd() method can be aliased as the trimRight() method which also removes whitespace characters from the right side or end of the string.

Example

Let's take a look at an example of how to use the trimEnd() method in JavaScript.

For example:

var totn_string = '   TechOnTheNet   ';

console.log(totn_string.trimEnd());

In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the trimEnd() method of the totn_string variable to remove the whitespace characters from the end of the string.

We have written the output of the trimEnd() method to the web browser console log, for demonstration purposes, to show what the trimEnd() method returns.

The following will be output to the web browser console log:

'   TechOnTheNet'

In this example, the trimEnd() method removed the whitespace characters from the end of the string and returned a string value of '   TechOnTheNet'.