Remove First and Last Characters from a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
2 min

banner

# Remove First and Last Characters from a String in JavaScript

To remove the first and last characters from a string, call the slice() method, passing it 1 and -1 as parameters.

The String.slice() method will return a copy of the original string with the first and last characters removed.

index.js
const str = 'abcd'; const withoutFirstAndLast = str.slice(1, -1); console.log(withoutFirstAndLast); // ๐Ÿ‘‰๏ธ bc

remove first and last characters from string

The code for this article is available on GitHub

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 of -1 means "go up to, but not including the last character in the string".
Passing an end index parameter of -1 and str.length - 1 is the same. We instruct the slice method to go up to, but not including the last character in the string.

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

Calling the slice() method on an empty string doesn't throw an error, it returns an empty string.

index.js
const str = ''; const withoutFirstAndLast = str.slice(1, -1); console.log(withoutFirstAndLast); // ๐Ÿ‘‰๏ธ ""

# Remove First and Last Characters from a String using String.substring()

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

index.js
const str = 'abcd'; const withoutFirstAndLast = str.substring(1, str.length - 1); console.log(withoutFirstAndLast); // ๐Ÿ‘‰๏ธ bc

remove first and last characters using substring

The code for this article is available on GitHub

We used the String.substring() method to achieve the same result.

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

If no end index is specified the slice goes to the end of the string.

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
The code for this article is available on GitHub

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

My personal preference is to use the String.slice() method because it's more intuitive than String.substring() in many different scenarios.

# 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