Remove the first Character from a String in JavaScript

avatar

Borislav Hadzhiev

Last updated: Oct 6, 2021

banner

Photo from Unsplash

Remove the first Character from a String #

To remove the first character from a string, call the slice method on the string, passing it 1 as a parameter, e.g. str.slice(1). The slice method will return a new string, containing a section of the original string, starting at index 1.

index.js
const str = 'hello world'; const withoutFirst = str.slice(1); console.log(withoutFirst); // 👉️ ello world

In the code snippet we passed 1 as the starting index to the String.slice method.

Indexes are zero-based in JavaScript, therefore the first character is at index 0.

By passing 1 as the starting index and no end index, we get back a new string containing all characters, but the first.

Note that the String.slice method does not mutate the original string. Strings are immutable in JavaScript.

An alternative and also very common approach is using the substring method.

To remove the first character from a string, call the substring method on the string, passing it 1 as a parameter, e.g. str.substring(1). The substring method will return a new string containing a portion of the string, starting at index 1.

index.js
const str = 'hello world'; const withoutFirst = str.substring(1); console.log(withoutFirst); // 👉️ ello world

In the code snippet we pass 1 as a starting index and no end index to the String.substring method to get a new string containing all characters, but the first.

For our purposes the slice and substring methods do the same, however there are some differences between the two for other use cases. If you want to read more on that, check out the section in the MDN docs.

As a personal preference I prefer the String.slice method because it's shorter, easier to read and more intuitive than String.substring for other use cases mentioned in the MDN docs.

Further Reading #

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.