EN
JavaScript - remove last character from string
5
points
In JavaScript, it is possible to remove the last character from a string in the following ways.
1. Using String slice()
method
// ONLINE-RUNNER:browser;
var text = 'abc';
var substring = text.slice(0, -1);
console.log(substring); // ab
2. Using String substring()
method
// ONLINE-RUNNER:browser;
var text = 'abc';
var substring = text.substring(0, text.length - 1);
console.log(substring); // ab
3. Using String replace()
method with regular expression
// ONLINE-RUNNER:browser;
var text = 'abc';
var substring = text.replace(/.$/, '');
console.log(substring); // ab
4. Using String replace()
method
// ONLINE-RUNNER:browser;
var text = 'abc';
var substring = text.replace('c', '');
console.log(substring); // ab