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?

Related Posts

  • 25 May

    Remove Double Quotes from String in JavaScript

    Table of ContentsWays to Remove Double Quotes from String in JavaScriptUsing replace()Using replaceAll()Using split() and join() methodUsing for loopConclusionWas this post helpful? In this post, we will see how to remove double quotes from String in JavaScript. Ways to Remove Double Quotes from String in JavaScript There are multiple ways to do it. Let’s go […]

  • 25 May

    Convert Seconds to Hours Minutes Seconds in Javascript

    Table of ContentsUsing javascript without any external libraryUsing momentJs libraryWas this post helpful? In this post, we will see how to convert seconds to Hours Minutes Seconds in JavaScript. There are multiple ways to do it. Let’s go through them. Using javascript without any external library To convert seconds to Hours Minutes Seconds in javascript: […]

  • 25 May

    [Solved] TypeError: toLowerCase is not a function in JavaScript

    TypeError: .toLowerCase is not a function occurs when we call toLowerCase() function on object which is not an string. toLowerCase() function can be only called on string. To resolve this issue, convert value to string using toString() ,method before calling toLowerCase() method. Let’s see with help of simple example [crayon-62b8e3f7e2d64118785941/] You will get following error […]

  • 25 May

    Escape Json String Containing Newline Characters in JavaScript

    Table of ContentsEscape JSON string in JavaScriptConclusionWas this post helpful? In this post, we will see how to escape JSON string containing newline characters using JavaScript. There is no well known JavaScript library which can escape all special characters in String. Escape JSON string in JavaScript There is no direct way to escape JSON String […]

  • 23 May

    Escape Apostrophe in JavaScript

    Table of ContentsWays to escape Apostrophe in JavaScriptUsing escape characterUsing alternate String syntaxUsing template literalsWas this post helpful? In this article, we will see how to escape Apostrophe in JavaScript. Let’s see what happens if we have singe quote in String and we also use single quotes to declare the string in JavaScript. [crayon-62b8e3f7e2f57622489777/] Output: […]

  • 23 May

    TypeError: foreach is not a function in JavaScript

    Table of ContentsUse Object.keys() or Object.values() or Object.entries()Use Arrays.from()Was this post helpful? TypeError: .foreach is not a function occurs when we call foreach() function on object which is not array, Set or Map. To resolve this issue, you can use Object.keys() rather than directly calling foreach() on object or use Array.from() to convert array like […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.