Table of Contents
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:
1 2 3 4 5 |
var str='Java2blog'; str = str.substring(1); console.log(str); |
Output:
1 2 3 |
ava2blog |
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()
1 2 3 4 5 6 7 8 |
var str='Java2blog'; // Remove first character only if it is equal to 'J' if(str.charAt(0) ==='J') str = str.substring(1); console.log(str); |
Output:
1 2 3 |
ava2blog |
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.
1 2 3 4 5 |
var str='Java2blog'; str = str.substring(3); console.log(str); |
Output:
1 2 3 |
a2blog |
Using slice()
To remove first character from string in JavaScript, call slice()
method on string with start index as 1.
Here is simple example:
1 2 3 4 5 |
var str='Java2blog'; str = str.slice(1); console.log(str); |
Output:
1 2 3 |
ava2blog |
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.
1 2 3 4 5 |
var str='Java2blog'; str = str.slice(3); console.log(str); |
Output:
1 2 3 |
a2blog |
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:
1 2 3 4 5 |
var str='Java2blog'; str = str.substr(1); console.log(str); |
Output:
1 2 3 |
ava2blog |
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.
1 2 3 4 5 |
var str='Java2blog'; str = str.substr(3); console.log(str); |
Output:
1 2 3 |
a2blog |
Using replace() method
To remove first character from string in JavaScript, get first character of String and replace it with empty String.
1 2 3 4 5 6 |
var str='Java2blog'; const firstChar = str.charAt(0); str = str.replace(firstChar,''); console.log(str); |
Output:
1 2 3 |
a2blog |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var str='Java2blog'; // Convert string to array const charArr = str.split(''); // Call shift on array charArr.shift(); // Join array back using join() method str= charArr.join(''); console.log(str); |
Output:
1 2 3 |
a2blog |
Conclusion
To remove first character from string in JavaScript, use substring()
, slice()
and substr()
.
Since substr()
is deprecated now, it is not recommended solution.