Languages
[Edit]
EN

JavaScript - remove last character from string

5 points
Created by:
Kate_C
2892

In JavaScript it is possible to remove last character from string in 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("abc", "ab");

console.log(substring); // ab

Merged questions

  1. JavaScript - how to chop off last char from string?
  2. How do I trim off last char in JavaScript?
  3. JavaScript - delete last character of string
  4. Slice off last character in js
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.