Remove the last N Characters from a String in JavaScript

avatar

Borislav Hadzhiev

Wed Oct 06 20212 min read

banner

Photo by Mike Erskine

Remove the last N Characters from a String in JavaScript #

To remove the last n characters from a string, call the slice() method, passing it 0 and -n as parameters. For example, str.slice(0, -3) returns a copy of the original string with the last 3 characters removed.

index.js
const str = 'hello world'; const withoutLast3 = str.slice(0, -3); console.log(withoutLast3); // ๐Ÿ‘‰๏ธ hello wo

We passed the following arguments to the String.slice method:

  1. start index - the index (zero-based) at which we start extraction.
  2. end index - extract characters up to, but not including this index. A negative index indicates an offset from the end of the string.
Passing an end index parameter of -3 and str.length - 3 is the same. We instruct the slice method to go up to, but not including the last 3 characters in the string.

Indexes are zero-based in JavaScript, which means that the index of the last character in a string is equal to str.length - 1.

Note that the slice method does not change the original string, it returns a new string.

A commonly used alternative to the slice method is String.substring.

To remove the last n characters from a string, call the substring() method passing it 0 and str.length - n as parameters. For example, str.substring(0, str.length - 3) returns a copy of the original string with the last 3 characters removed.

index.js
const str = 'hello world'; const withoutLast3 = str.substring(0, str.length - 3); console.log(withoutLast3); // ๐Ÿ‘‰๏ธ hello wo

The substring method also takes the start and end indexes as parameters.

However, if you pass a negative end index to the substring method, it treats it as if you passed 0, which is a very confusing behavior.

The substring method does not mutate the contents of the original string.

In my experience the substring method is more widely used in older code bases and the slice method is more often used in newer code.

My personal preference is to use the slice method, because the substring method has some confusing and hard to debug nuances.

For example, if you pass a greater start index than end index to the substring method, it reversed them, whereas the slice method just returns an empty string, which is what you'd expect.

index.js
const str = 'hello world'; const withSlice = str.slice(100, 1); console.log(withSlice); // ๐Ÿ‘‰๏ธ "" const withSubstring = str.substring(100, 1); console.log(withSubstring); // ๐Ÿ‘‰๏ธ ello world

You can see that the substring method assumed that we got the ordering of the parameters wrong and decided to help us by "correcting" the order.

In the example it practically sets the start index to 1 and the end index to 100.

If you want to read more about the differences between slice and substring, check out this section in the MDN docs.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee