You can use the substring()
and slice()
methods to remove one or more characters at the start of a string in JavaScript.
Remove the first character from a string
You can use the substring()
method to remove the first character from a string. The str.substring(1)
returns a new string without the first character of the original string.
const str = 'JavaScript'
const result = str.substring(1)
console.log(result) // avaScript
The substring()
method extracts characters between the start and end indexes from a string and returns the substring.
Indexes in JavaScript are zero-based. The first character in a string has an index of
0
, and the last has an index ofstr.length - 1
.
We passed 1
as a starting index to the substring()
method to exclude the first character J
from the returned string. The index of 1
specifies that we want to skip the characters at indexes 0
and 1
and get a new string containing the rest.
Alternatively, you could use the slice()
method to remove the first character from a string, as shown below:
const str = 'JavaScript'
const result = str.slice(1)
console.log(result) // avaScript
The slice()
method extracts a part of a string between the start and end indexes, specified as first and second parameters. It returns the extracted part as a new string and does not change the original string.
Remove the first N characters from a string
You can also use the substring()
method to remove the first N characters from a string in JavaScript:
const str = 'JavaScript'
const removed2 = str.slice(3)
console.log(removed2) // aScript
const removed6 = str.slice(6)
console.log(removed6) // ript
const removed9 = str.slice(9)
console.log(removed9) // t
As you can see above, we omitted the end index and only passed a starting index to substring()
to remove the first N characters from a string.
The slice()
method can also be used to remove the first N characters from a string:
const str = 'JavaScript'
const removed4 = str.slice(4)
console.log(removed4) // Script
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.