Skip to content

Remove the last character from the string JavaScript

  • by

Use the slice() method to remove the last character from the string JavaScript. You must pass two arguments: the start and end indexes.

str.slice(0, -1)

Use slice(0, -1), it is equivalent to slice(0, str.length - 1).

Remove last character JavaScript

Simple example code.

<!DOCTYPE html>
<html lang="en">
    let str = "12345.00";

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


Remove the last character from the string JavaScript

Alternative Methods

Other methods available are substring() and replace(). substring() does not have negative indexing, so be sure to use str.length - 1 when removing the last character from the string.

let str = '';
str.substring(0, str.length - 1); //
str.substr(0, str.length - 1); //

replace() takes either a string or a regular expression as its pattern argument.

let str = ''; 
str.replace(/.$/, ''); //

Remove the last character only if the last character is a number. You can use .replace(/\d$/, '') as shown below.

let str = 'abc.io0';
str.replace(/\d$/, ''); //

Do comment if you have any doubts or suggestions on this Js remove char topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

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