How to Remove the Last Character from a String in JavaScript? A Step-by-Step Guide for Beginners and Pros
Have you ever encountered a situation where you need to remove an extra comma at the end of a string, delete a period, or simply "chop off" the last character? This question is a real hit on Stack Overflow with hundreds of thousands of views. And for good reason: working with strings is the foundation upon which 90% of frontend development rests.
In this article, we won't just give you dry answers. We'll break down 5 working methods, show the pitfalls, and teach you how to choose the right tool for each task. Let's go!
Why is this important? Three real-life scenarios
Before diving into the code, let's imagine situations where you will definitely need to remove the last character from a JavaScript string:
- Formatting lists: You join an array into a string using
join(', '), but there's an extra comma at the end. - Processing URLs: You need to remove a trailing slash before sending the address to the server.
- Working with IDs: You need to remove the last hyphen from a string like "item-123-".
Sounds familiar? Then let's dive into the methods for removing the last character in JavaScript.
Method #1: slice() — the king of simplicity and speed
This is the most popular and intuitive method. The slice() method returns a new array (or string), copying elements from a specified position.
let str = "Hello, world!";let result = str.slice(0, -1);console.log(result); // "Hello, world"How does it work? slice(0, -1) means: "take all characters starting from index 0, and stop 1 character before the end." Negative indices in JavaScript count from the end of the string. Ingenious and concise!
Pros and cons of slice():
- Pro: Works with both strings and arrays. Does not modify the original string.
- Pro: The fastest method in 95% of cases (according to JSperf tests).
- Con: Not suitable if you need to remove not the last character, but, say, the last 3.
slice() as your primary tool. It's the industry standard.Method #2: substring() — an old but reliable friend
The substring() method is as old as JavaScript. It works similarly, but with one important difference: it doesn't understand negative indices and simply converts them to 0.
let str = "JavaScript";// To remove the last character, you need to know the lengthlet result = str.substring(0, str.length - 1);console.log(result); // "JavaScrip"See? You have to calculate the length of the string. This is inconvenient, but historically, substring() is often found in legacy code.
When to use substring?
- If you need support for very old browsers (IE 8 and below — but this is rare).
- If you are writing educational code and want to explicitly show the logic of working with indices.
Important: substring() swaps its arguments if the first is greater than the second. slice() will return an empty string in this case. Be careful!
Method #3: substr() — deprecated but persistent
Yes, substr() is officially deprecated in the ECMAScript specification. But you will still find it in millions of lines of code on GitHub.
let str = "Delete!";let result = str.substr(0, str.length - 1);console.log(result); // "Delete"The difference from substring() is that the second argument is the length of the extracted part, not the ending index. Confusing? A little.