Web Tools Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu June 03, 2023 › #HowTo #Javascript

How to remove last character from string in JavaScript

In this post, you will learn how to remove the last character from a string in JavaScript.

Remove last character from a string using slice() method.

Example using slice() method
    let myString = "This is a string";
    document.write(myString.slice(0, -1)); 
Try it Yourself »
The slice() method returns the extracted part in a new string. It does not change the original string.

Remove last character from a string using substring() method.

Example using substring() method
    let myString = "This is a string";
    document.write(myString.substring(0, myString.length - 1));
Try it Yourself »

Remove last character from a string using replace() method.

Example using replace() method
  
  let myString = "This is a string";
  document.write(myString.replace("g", "")); 
 
Try it Yourself »

Using substr() method.

Example of substr() method
  let myString = "This is a string";
  document.write(myString.substr(0, myString.length - 1);  
Try it Yourself »
save
listen
AI Answer
Write Your Answer
loading
back
View All