JavaScript Code


How to remove last character from String?


Remove last character from String in JavaScript

Given a string value, remove last character from the string, and return the resulting string.

Solution

To remove last character from the given string in JavaScript, use String.substr() method. substr() method can take index, and length of substring as arguments, and returns the substring from the specified index for the given length. Provide the length of substring, such that the last character is not included.

str.substr(0, str.length - 1)

Program

1. Remove last character from the string value in str.

let str = 'helloworld';
let output = str.substr(0, str.length - 1);
console.log(output);