You can use the substring() method to remove the last character of a string in JavaScript. The first argument of the method is the start index, and the second argument is the end index. To remove the last character, you can use the length of the string as the start index and the length of the string minus one as the end index.

let str = "Hello, world!";
str = str.substring(0, str.length - 1);
console.log(str); // "Hello, world"

Another way is to use the slice() method, it works the same way

let str = "Hello, world!";
str = str.slice(0, -1);
console.log(str); // "Hello, world"

You can also use the substr() method, where the first parameter is the start index and the second parameter is the number of characters to remove

let str = "Hello, world!";
str = str.substr(0, str.length - 1);
console.log(str); // "Hello, world"

You can also use the replace() method to remove the last character of a string in JavaScript. This way you can remove any character from the string

let str = "Hello, world!";
str = str.replace(/.$/,'')
console.log(str); // "Hello, world"

Also Read:

Categorized in: