How do I remove the last character from a string in JavaScript?
I have a string, 12345.00, and I would like it to return 12345.0.
I have looked at trim(), but it seems to only remove whitespace. I’ve also considered slice(), but I’m not sure how to use it for this purpose. What’s the best way to remove the last character from a string in JavaScript?
To remove the last character from a string in JavaScript, you can use the slice() method by calling slice(0, -1) which extracts all characters except the last one. For your specific example "12345.00", using slice() will return "12345.0" as desired. Alternatively, you can use substring() by specifying the start index as 0 and end index as string.length - 1.
Contents
- Using the slice() Method
- Using the substring() Method
- Other Alternative Methods
- Practical Examples and Best Practices
- Performance Considerations
Using the slice() Method
The slice() method is one of the most elegant ways to remove the last character from a string. It extracts a section of a string and returns it as a new string, without modifying the original.
const originalString = "12345.00";
const result = originalString.slice(0, -1);
console.log(result); // "12345.0"
How it works:
- The first parameter
0indicates the starting position (beginning of the string) - The second parameter
-1tells JavaScript to go up to, but not including, the last character - This method is very readable and concise
Note:
slice()is non-destructive, meaning it doesn’t modify the original string but returns a new one.
The slice() method is particularly useful because:
- It handles empty strings gracefully without throwing errors
- It’s widely supported across all modern browsers
- It’s chainable with other string methods
Using the substring() Method
The substring() method is another reliable option for removing the last character. It works similarly to slice() but uses positive indices.
const originalString = "12345.00";
const result = originalString.substring(0, originalString.length - 1);
console.log(result); // "12345.0"
How it works:
originalString.length - 1calculates the position of the second-to-last character- The substring extracts from index 0 up to (but not including) this calculated index
Key differences from slice():
substring()doesn’t support negative indices, so you need to calculate the length manually- Both methods produce the same result for this use case
According to the Mozilla Developer Network documentation, substring() is also non-destructive and returns the extracted portion as a new string.
Other Alternative Methods
While slice() and substring() are the most common approaches, there are several other ways to achieve the same result:
Using substr() (Deprecated)
The substr() method was deprecated in ECMAScript 2021 but is still supported in some environments:
const originalString = "12345.00";
const result = originalString.substr(0, originalString.length - 1);
console.log(result); // "12345.0"
Warning:
substr()is deprecated and should be avoided in new code. Useslice()orsubstring()instead.
Using Regular Expressions
Regular expressions provide a powerful way to remove the last character:
const originalString = "12345.00";
const result = originalString.replace(/.$/, '');
console.log(result); // "12345.0"
How it works:
- The regex
.$/matches any character (.) at the end of the string ($) - The empty string
''as the replacement removes the matched character
Using String Concatenation
You can also use string concatenation with substring():
const originalString = "12345.00";
const result = [originalString.substring(0, originalString.length - 1)].join('');
console.log(result); // "12345.0"
Practical Examples and Best Practices
Here are some practical examples showing how to apply these methods in different scenarios:
Handling Edge Cases
// Empty string
const emptyString = "";
const emptyResult = emptyString.slice(0, -1);
console.log(emptyResult); // "" (returns empty string, no error)
// Single character string
const singleChar = "A";
const singleResult = singleChar.slice(0, -1);
console.log(singleResult); // "" (removes the character)
// String with multiple characters
const multiChar = "Hello World";
const multiResult = multiChar.slice(0, -1);
console.log(multiResult); // "Hello Worl"
Function for Reuse
function removeLastCharacter(str) {
if (str.length === 0) return str;
return str.slice(0, -1);
}
// Usage examples
console.log(removeLastCharacter("12345.00")); // "12345.0"
console.log(removeLastCharacter("test")); // "tes"
console.log(removeLastCharacter("A")); // ""
console.log(removeLastCharacter("")); // ""
Chaining with Other Methods
const originalString = " 12345.00 ";
const result = originalString.trim().slice(0, -1);
console.log(result); // "12345.0"
Performance Considerations
When choosing between different methods, consider the following performance aspects:
Method Comparison
| Method | Performance | Readability | Browser Support |
|---|---|---|---|
slice(0, -1) |
Excellent | High | Universal |
substring(0, length-1) |
Excellent | Medium | Universal |
substr(0, length-1) |
Good | Medium | Universal (deprecated) |
| Regex | Moderate | Low | Universal |
Performance Benchmark
Based on various performance tests, slice() and substring() are generally the fastest methods for removing the last character, with slice() often having a slight edge in modern JavaScript engines.
Sources
- MDN Web Docs - String.prototype.slice()
- MDN Web Docs - String.prototype.substring()
- W3Schools - JavaScript String Methods
- JavaScript.info - Strings
Conclusion
To remove the last character from a string in JavaScript, slice() is the recommended approach due to its simplicity and readability. For your specific example "12345.00", simply use:
const result = "12345.00".slice(0, -1);
// Result: "12345.0"
Key takeaways:
- Use
slice(0, -1)for the cleanest and most readable solution - Consider edge cases like empty strings or single-character strings
- Avoid deprecated methods like
substr()in new code - Both
slice()andsubstring()provide excellent performance
For most use cases, slice() will be your best choice when you need to remove the last character from a JavaScript string.