Remove the Last Character from a String in JavaScript

Use substring() and slice() to remove the last character from a string in JavaScript.

1. Using substring()

The substring() method will return the string between the specified start and end indexes.

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

2. Using slice()

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); 

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *