Remove First Character from String in JavaScript

In this post, we will see how to remove first character from string in JavaScript.

Since Strings are immutable in JavaScript, you can’t do it in-place. All the methods will return new String and you can either assign it to same variable or to new one.

Ways to Remove First Character from string in JavaScript

There are multiple ways to remove first character from String in JavaScript. Let’s go through them.

Using substring()

To remove first character from string in JavaScript, call substring() method on string with start index as 1.

Here is simple example:

Output:

string’s substring() method returns part of the String between start and end indexes or to the end of String if end index is not provided.

If you want to put some condition on first character, then you can use charAt() with substring()

Output:

You can use this solution to remove first n characters from String.

For example:
Let’s say you want to remove first 3 characters of the String. You can pass start index as 3 and substring() will return remaining string after first 3 characters.

Output:

Using slice()

To remove first character from string in JavaScript, call slice() method on string with start index as 1.

Here is simple example:

Output:

string’s slice() method extracts part of the String and returns new string without changing original string.

You can use this solution to remove first n characters from String.

For example:
Let’s say you want to remove first 3 characters of the String. You can pass start index as 3 and slice() will return remaining string after first 3 characters.

Output:

Using substr()

To remove first character from string in JavaScript, call substr() method on string with start index as 1.

substr() is deprecated method now and should be avoided.

Here is simple example:

Output:

string’s substr() method returns portion of the String and extending for given number of characters after that index. If length is not defined, then it will return portion of string upto the end.

You can use this solution to remove first n characters from String.

For example:
Let’s say you want to remove first 3 characters of the String. You can pass start index as 3 and substr() will return remaining string after first 3 characters.

Output:

Using replace() method

To remove first character from string in JavaScript, get first character of String and replace it with empty String.

Output:

Please note that replace() method is only going to replace first occurence of the character and that’s what we require here.

Using Array’s shift() with split() and join()

To remove first character from string in JavaScript, convert string to array using split() method, call shift on array and convert back to string using join() method.

Output:

Conclusion

To remove first character from string in JavaScript, use substring(), slice() and substr().

Since substr() is deprecated now, it is not recommended solution.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *