How To Remove First Character From A String in JavaScript

Do you want to remove First Character from a String in JavaScript? In this article, I will discuss different methods to remove the first character from a string in JavaScript.

There are various inbuilt functions present in the String Class of JavaScript that you can use to remove the character from a given string. One such method is a slice(), this method removes the character you want to remove from a string. Let us see the examples below.

1. Using the Slice Method in JavaScript To Remove First Character From a String

The slice[1] method is part of the string class. When the slice() method is called, it extracts a segment of a string and returns it as a new string, without altering the original string.

Let us see in the below example code the usage of the slice() method in JavaScript to remove the first character.

//Initializing the String
var givenString = "xCoduber.com"

//removing the first Character x from the
//above string to get Coduber.com
givenString = givenString.slice(1)

//Printing the String After Removing the First Character
console.log(givenString)

Output:

Coduber.com

As you can see in the above code, I was able to remove the first character by using the input as 1 that indicates that the string character needs to be removed starting from index 0 and ends before index 1. Hence this slice() method only removes the first character from the String.

You can also use the below line of code for a better understanding of the slice() method discussed above.

givenString = givenString.slice(1, givenString.length)

From the above code snippet, it is clear that the string slicing needs to start from index 1 and it ends till the end of the length of the string. This removes the first character from the string.

2. Using the substr Method From the String Class

There is another method present in the String Class of JavaScript substr that lets to remove the first character from the string. Basically, here substr creates the substring for provided length. It takes to start and end as an argument where the start is the index number to start the substr and end is the last index value were to end the substr.

Let us see in the below example code the usage of substr to remove the first character from a string in JavaScript.

//Initializing the String
var givenString = "xCoduber.com"

//removing the first Character x from the
//above string to get Coduber.com
givenString = givenString.substr(1, givenString.length)

//Printing the String After Removing the First Character
console.log(givenString)

Output:

Coduber.com

As you can see the substr method also works the same as the above method slice. It takes the same number of arguments and returns the string after removing the first character from the given String.

3. Using substring Method from String Class

String Class in JavaScript has another method named substring[2]. The substring() method returns the portion of the string that is between the start and end indexes, or the portion of the string that is to the end of the string, respectively.

Let us see in the below example code the usage of the substring Method to remove a character from a String using JavaScript.

//Initializing the String
var givenString = "xCoduber.com"

//removing the first Character x from the
//above string to get Coduber.com
givenString = givenString.substring(1, givenString.length)

//Printing the String After Removing the First Character
console.log(givenString)

Output:

Coduber.com

I do not think I need to explain this method to you as it is almost similar to the substr method discussed above. The only difference I observed is this method runs faster than the substr method.

4. Using the Replace Method from String Class

Another Method replace() present in the String Class can also be used to remove the first character from a string. But the difference in this method is that you need to enter the first character as an argument and the second argument to the function will be the string you want to replace the first character from.

As we are removing the first character from the string hence we will set an empty string to replace the first character. Let us see in the below example code the usage of replace method to remove a character from a string.

//Initializing the String
var givenString = "xRemove First Character"

//removing the first Character x from the
//above string to get Coduber.com
givenString = givenString.replace(givenString[0], "")

//Printing the String After Removing the First Character
console.log(givenString)

Output:

Remove First Character

As you can see in the above code, I provided the first character of the string as the first argument to the replace method and an empty string as to the second argument to the method.

And the replace method removes or replaces the first character from the existing string to an empty string and provides the required output.

How To Remove First Character From A String in JavaScript

Wrap Up

I hope you were able to learn how to remove the first character from a string in JavaScript. I have discussed a total of four different methods that can be used to achieve the removal of character from a string.

Let me know in the comment section if you know any better method than the one discussed above I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Fix Cannot use import statement outside a module JavaScript
  2. How To Remove A Specific item From An Array
  3. Best Udemy JavaScript Course 2021

Leave a Comment