How to remove the first character of a string in JavaScript
By Flavio Copes
Learn how to remove the first character of a string in JavaScript with the slice() method, passing 1 as the argument, which returns a new unmodified string.
To remove the first character of a string in JavaScript, call the slice() method on the string, passing 1 as the argument:
const text = 'abcdef'
const editedText = text.slice(1) //'bcdef'
slice(1) returns the portion of the string starting at index 1, which is everything except the first character.
Strings are immutable
Note that the slice() method does not modify the original string. Strings in JavaScript can’t be changed in place: every string method returns a new string, and the original stays as it was.
const text = 'abcdef'
text.slice(1)
text //'abcdef'
This is why I assign the result to a new variable in the first example. If you forget the assignment, the result is computed and thrown away.
What about short strings?
Calling slice(1) on an empty string, or on a string with a single character, returns an empty string. No error is thrown:
''.slice(1) //''
'a'.slice(1) //''
That makes the technique safe to use without checking the length first.
An alternative is substring(1), which behaves the same way for this case. I prefer slice() because it also accepts negative indexes, so the same method handles the end of the string too:
'abcdef'.slice(0, -1) //'abcde'
Watch out for emoji
There’s one pitfall. slice() counts UTF-16 code units, not characters as you see them. Most characters take one code unit, but emoji take two. Removing “the first character” from a string that starts with an emoji cuts it in half:
const label = '🧑abc'
label.slice(1) //'�abc'
The result starts with a lone surrogate, which displays as garbage.
If your strings can contain emoji, split them with Array.from() first. It splits the string by full characters, so slicing the resulting array is safe:
Array.from('🧑abc').slice(1).join('') //'abc'
For plain text like identifiers, prefixes, or anything you control, slice(1) is all you need.
Related posts about js: