In this article we will learn how to remove first letter of a string.
It is very common to remove first letter of a string in JavaScript.
There is more than one way to solve this problem:
- Using
slice()
method - Using
subString()
method
Problem
Now we have a problem, which is that we want to remove first letter of the string.
Imagine we have a string like this:
"Hello World"
And we want to remove the first letter so that the string becomes like this
"ello World"
How to solve this problem?
As we said before, there's more than one way to solve this problem such as:
- Using
slice()
method - Using
subString()
method
Remove first letter using slice
method
We can use slice
method to remove first letter of string.
Example
// String
const string = "Hello World";
// Remove first letter
const result = string.slice(1);
// Result:
console.log(result);
Output
ello World
The slice()
method returns a shallow copy of a portion of an array into a new array object selected from start to end (Source: MDN)
Remove first letter using subString
We can use subString
to remove first letter of a string as well.
Example
// String
const string = "Hello World";
// Remove first letter
const result = string.substring(1);
// Result:
console.log(result);
Output
ello World
The substring()
method returns the part of the string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied. (Source: MDN)
Thank you for reading
Thank you for reading my blog. 🚀