Skip to main content

How to Remove the Last N Characters from a String in JavaScript

Removing a specific number of characters from the end of a string is a common text manipulation task. You might need to strip a file extension, remove a trailing slash from a URL, or simply truncate a string for display. The standard, most idiomatic, and readable way to achieve this is with the String.prototype.slice() method, which excels at this thanks to its support for negative indices.

This guide will teach you how to use slice() to remove the last N characters from any string, explain why it's the best tool for the job compared to substring(), and show you a conditional approach using replace() for when you only want to remove a specific suffix.

The slice() method extracts a section of a string and returns it as a new string, leaving the original unchanged. To remove the last N characters, you provide 0 as the start index and a negative number for the end index.

Problem: you have a string and want to remove a specific number of characters from the end.

// Problem: How to remove the last 4 characters (".com") from this string?
const domain = 'example.com';

Solution: call slice() with a start index of 0 and an end index equal to the negative number of characters you want to remove.

const domain = 'example.com';

// To remove the last 4 characters, the end index is -4.
const newString = domain.slice(0, -4);

console.log(newString); // Output: 'example'
console.log(domain); // Output: 'example.com' (The original is unchanged)

Output:

example
example.com
note

This is the most concise and modern way to solve this problem.

How slice() Works with a Negative Index

The slice(startIndex, endIndex) method takes two arguments:

  • startIndex: The index where the new string begins. 0 means we start from the very beginning.
  • endIndex: The index before which to end the extraction. A negative index is the key feature here; it counts from the end of the string. So, -4 means "up to, but not including, the fourth character from the end."
note

This negative index support is what makes slice() superior to other methods for this task, as you don't need to manually calculate the end position using the string's length.

Handling Edge Cases

The slice() method is very safe. If you try to remove more characters than the string has, it simply returns an empty string ('') without throwing an error.

const str = 'abc';
console.log(str.slice(0, -5)); // Output: ''

What About substring()?

The String.prototype.substring() method cannot accept negative indices. To use it, you must manually calculate the end position, which is more verbose and less readable.

const domain = 'example.com';
// This works, but it's less direct than slice(0, -4).
const newString = domain.substring(0, domain.length - 4);
note

For this reason, slice() is the clear winner for this specific task.

Conditional Removal with replace() and a Regular Expression

Sometimes, you only want to remove the last N characters if they match a specific suffix. The replace() method with a regular expression is the perfect tool for this.

Problem: you want to remove the .pdf extension from a filename, but only if it's actually there. You don't want to accidentally remove the last 4 characters from a file named mydocument.

Solution: use a regular expression with a dollar sign ($) anchor, which means "match only at the end of the string."

function removePdfExtension(filename) {
// The regex /\.pdf$/ matches the literal string ".pdf" at the end of the string.
return filename.replace(/\.pdf$/, '');
}

// Example Usage:
const file1 = 'report.pdf';
console.log(removePdfExtension(file1)); // Output: 'report'

const file2 = 'archive.zip';
// The suffix doesn't match, so the string is returned unchanged.
console.log(removePdfExtension(file2)); // Output: 'archive.zip'

Output:

report
archive.zip

Conclusion

Removing the last N characters from a string is a simple task with a clear, modern solution.

  • To unconditionally remove the last N characters, the recommended best practice is string.slice(0, -N). It is concise, readable, and its use of a negative index is highly intuitive for this task.
  • To conditionally remove the last N characters only if they match a specific suffix, the best tool is string.replace(/suffix$/, '') using a regular expression anchored to the end of the string.