How to Remove the Last Character from a String in JavaScript


Let’s see how we can use substring() and slice() to remove the last character from a string in JavaScript.

1. Using substring()

If two arguments are supplied, the substring() method will return the string between the specified start and end indexes.

str = str.substring(0, str.length - 1);

2. Using slice()

If two arguments are supplied, the slice() method will also return the string between the specified start and end indexes.

A negative index can be used to indicate an offset from the end of the sequence. -1 would indicate that we only want elements up until the second-to-last element in the sequence.

str = str.slice(0, -1);