Remove Last Character From String in Javascript

Posted by Will on November 13, 2012

Its a simple matter to remove the last character from a string in Javascript. There are 2 ways to go about this, either works fine.


var theString = 'Angus Macgyver!';
var theStringMinusOne = theString.substring(0, theString.length-1);

That should pop up “Angus Macgyver”, without the exclamation point. And the same functionality, different function…..


var theString = 'Angus Macgyver!';
var theStringMinusOne = theString.slice(0, -1);

I personally like the first option as substring is a familiar function across various languages. Honestly, there’s no difference though, pick your pleasure.

Leave a Reply

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