Remove the last N Characters from a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
5 min

banner

# Table of Contents

  1. Remove the last N Characters from a String in JavaScript
  2. Remove the last N Characters from a String using String.substring()
  3. Remove the last N characters from a string conditionally

# Remove the last N Characters from a String in JavaScript

To remove the last N characters from a string, call the slice() method with a start index of 0 and an end index of -N.

For example, str.slice(0, -2) will return a new string with the last 2 characters of the original string removed.

index.js
const str = 'bobbyhadz.com'; // โœ… Remove the last 2 characters from a string const removeLast2 = str.slice(0, -2); console.log(removeLast2); // ๐Ÿ‘‰๏ธ bobbyhadz.c // โœ… Remove the last 3 characters from a string const removeLast3 = str.slice(0, -3); console.log(removeLast3); // ๐Ÿ‘‰๏ธ bobbyhadz.

remove last n characters from string

The code for this article is available on GitHub

The String.slice() method extracts a section of a string and returns it, without modifying the original string.

The String.slice() method takes the following arguments:

NameDescription
start indexThe index of the first character to include in the returned substring
end indexThe index of the first character to exclude from the returned substring

The String.slice() method can be passed a negative index to count backward.

index.js
const str = 'bobbyhadz.com'; // โœ… Remove the last 3 characters from a string console.log(str.slice(0, -3)); // ๐Ÿ‘‰๏ธ bobbyhadz.

Using an end index 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.

index.js
const str = 'bobbyhadz.com'; // โœ… Remove the last 3 characters from a string const removeLast3 = str.slice(0, -3); console.log(removeLast3); // ๐Ÿ‘‰๏ธ bobbyhadz. // ------------------------------------------------- // โœ… Same as above const removeLast3_ = str.slice(0, str.length - 3); console.log(removeLast3_); // ๐Ÿ‘‰๏ธ bobbyhadz.
JavaScript indexes are zero-based, so the index of the first character in a string is 0 and the index of the last character is equal to str.length - 1.

Note that the String.slice() method doesn't change the original string, it returns a new string. Strings are immutable in JavaScript.

If you try to remove more characters than there are in the string, the slice() method returns an empty string.

index.js
const str = 'bobby'; // โœ… Remove the last 100 characters from a string const removeLast100 = str.slice(0, -100); console.log(removeLast100); // ๐Ÿ‘‰๏ธ ""

We tried to remove the last 100 characters from a string that only contains 5 characters, so the slice() method returned an empty string.

If you have to do this often, define a reusable function.

index.js
function removeLastNchars(str, n) { return str.slice(0, -n); } const str = 'bobbyhadz.com'; const removeLast2chars = removeLastNchars(str, 2); console.log(removeLast2chars); // ๐Ÿ‘‰๏ธ bobbyhadz.c const removelast3chars = removeLastNchars(str, 3); console.log(removelast3chars); // ๐Ÿ‘‰๏ธ bobbyhadz.

The removeLastNchars function takes a string and n as parameters and removes the last N characters from the string.

# Remove the last N Characters from a String using String.substring()

Alternatively, you can use the String.substring() method.

index.js
const str = 'bobbyhadz.com'; // โœ… Remove the last 2 characters from a string const removeLast2 = str.substring(0, str.length - 2); console.log(removeLast2); // ๐Ÿ‘‰๏ธ bobbyhadz.c // โœ… Remove the last 3 characters from a string const removeLast3 = str.substring(0, str.length - 3); console.log(removeLast3); // ๐Ÿ‘‰๏ธ bobbyhadz.

remove last n characters from string using substring

The code for this article is available on GitHub

The String.substring() method returns a slice of the string from the start index to the excluding end index.

The method takes the following parameters:

NameDescription
start indexThe index of the first character to include in the returned substring
end indexThe index of the first character to exclude from the returned substring

Notice that the substring() method doesn't support negative indexing.

The end index is exclusive (up to, but not including), so specifying an end index of str.length - 2 means "go up to, but not including the last 2 characters of the string".

We used the slice() and substring() methods in a similar way to remove the last N characters from a string.

However, there are a couple of differences between the String.substring() and the String.slice() methods:

  • The substring() method swaps its start and end index if the start index is greater than the end index. The slice() method returns an empty string in this case.
index.js
const str = 'bobby'; console.log(str.substring(3, 0)); // ๐Ÿ‘‰๏ธ bob console.log(str.slice(3, 0)); // ๐Ÿ‘‰๏ธ ''
  • If either of both arguments passed to substring() are negative, they are treated as if they were 0.
index.js
const str = 'bobby'; console.log(str.substring(-3)); // ๐Ÿ‘‰๏ธ bobby console.log(str.slice(-3)); // ๐Ÿ‘‰๏ธ bby

When given a negative index, the slice() method counts backward from the end of the string to find the indexes.

The slice() method is implemented more intuitively and should be your approach.

# Remove the last N characters from a string conditionally

Use the String.replace() method to remove the last N characters from a string conditionally.

The method will only remove the last N characters from the string if the string ends with the specified characters.

index.js
const str = 'bobbyhadz.com'; // โœ… remove the last 2 characters from a string conditionally const removeLast2 = str.replace(/om$/, ''); console.log(removeLast2); // ๐Ÿ‘‰๏ธ bobbyhadz.c // โœ… remove the last 3 characters from a string conditionally const removeLast3 = str.replace(/com$/, ''); console.log(removeLast3); // ๐Ÿ‘‰๏ธ bobbyhadz.

remove last n characters from string conditionally

The code for this article is available on GitHub

The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.

The first argument we passed to the String.replace() method is a regular expression.

The forward slashes / / mark the beginning and end of the regular expression.

index.js
const str = 'bobbyhadz.com'; const removeLast2 = str.replace(/om$/, ''); console.log(removeLast2); // ๐Ÿ‘‰๏ธ bobbyhadz.c

We then specified the characters om and a dollar sign $.

The dollar sign $ matches the end of the input.

In other words, the om characters are matched only if they are at the end of the string.

The second argument we passed to the replace() method is the replacement string.

We replace the match with an empty string in order to remove the characters.

This approach is useful when you only want to remove the last N characters from a string if the string ends with the specified characters.

If the string doesn't end with the specified characters, the entire string is returned as is.

index.js
const str = 'bobbyhadz.com'; const result = str.replace(/bobby$/, ''); console.log(result); // ๐Ÿ‘‰๏ธ bobbyhadz.com

The String.replace() method returns a new string with the matches of the pattern replaced. The method doesn't change the original string.

Strings are immutable in JavaScript.

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev