This post will discuss how to remove the last character from a string in JavaScript.

It is not possible to in-place remove characters from a string since strings are immutable in JavaScript. The idea is to create a new string instead.

 
There are several ways to do that in JavaScript using the substring(), slice(), or substr() method.

1. Using substring() method

The substring() method returns the part of the string between the specified indexes. You can use it as follows to remove the last character from a string:

Download  Run Code

2. Using slice() method

The slice() method extracts the text from a string between the specified indexes and returns a new string. You can use the following code, which removes the last character from the string:

Download  Run Code

 
The following example uses slice() with negative indexes.

Download  Run Code

3. Using substr() method

The substr() method returns a portion of the string, starting at the specified index and extract the specified number of characters. The substr() method is considered a legacy function, and we should use the substring() method instead.

Download  Run Code

That’s all about removing the last character from a string in JavaScript.