- Instant help with your JavaScript coding problems

Remove the last character from a string in JavaScript

Question:
How to remove the last character from a string in JavaScript?
Answer:
const removeLastCharacter = (text) => {
    if (!text) {
        return '';
    }
    return text.slice(0, -1);
}
Description:

If you just want to remove the last character of a string, you can use the slice method with startIndex set to 0 and endIndex set to -1 .

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string. If endIndex is negative, slice() treats it as str.length + endIndex .

 

Share "How to remove the last character from a string in JavaScript?"
Related snippets:
Tags:
remove, trim, last, character, string, text, javascript, delete, remove last character
Technical term:
Remove the last character from a string in JavaScript