How to Remove First Character from String in Javascript

To remove the first character from the string in JavaScript, use the slice() method. The string.slice() is a built-in JavaScript method that extracts a part of a string. The slice() method returns the extracted part in a new string. The slice() method does not change the original string.

Syntax

string.slice(startIndex, endIndex);

Parameters

start index –  It is a required parameter that needs to pass a starting index number of the string.

end index – It is an optional argument, and we can give an ending index number of the string.

Example

let string = "Greetings from JavaScript";
string = string.slice(1)
console.log(string);

Output

reetings from JavaScript

In the above example, we remove the first character using the slice() method, so the slice() method returns a shallow copy of that string, and we store that string in the original string.

Removing the first character using the substring() method

You can use the substring() method to remove the first character of a string in JavaScript. The substring() method also accepts two parameters: one is the starting number of the index, and the second is the endindex number. But if we pass only one parameter so, by default, it will consider the first parameter as 0.

Syntax

string.substring(startIndex, endIndex);

Parameters

start index –  We can pass the starting index number of the string. It is a required parameter.

end index – We can give an ending index number of the string. This parameter is optional.

Example

let string = "Greetings from Javascript";
string = string.substring(1)
console.log(string);

Output

reetings from Javascript

Using the substring () method, we removed the first character, “G,” from the string using the substring() method.

Removing the first character using the shift() method

JavaScript shift() is a built-in method that removes the first element from an array and returns that removed element. To convert a string to an array in JavaScript, use the string.split() method and then use the shift() method to remove the first character and then use the array.join() method to convert an array to a string.

The shift() method changes the length of the array.

Syntax

array.shift()

Parameters

It does not take any argument.

Example

let string = "Greetings from JavaScript";
const refStringArray = string.split("");
refStringArray.shift();
const result = refStringArray.join('');
console.log("Removed first character from string:-", result);

Output

Removed first character from string:- reetings from JavaScript

We removed the first character using the shift() method by converting an array and then convert back to a string.

Using String.replace() method

The String.replace() is a built-in JavaScript method that searches a string for a value or a regular expression. It only replaces the first occurrence of the matched string.

Syntax

string.replace(searchValue, newValue)

Arguments

The searchValue is the required parameter: a value or regular expression which is to search for.

The newValue is the required parameter that is the value to replace with.

Example

let string = "Greetings from Javascript";
let firstChar = string.charAt(0);
const newString = string.replace(firstChar, '');
console.log(newString)

Output

reetings from Javascript

Conclusion

The most optimal way to remove the first character from a string in JavaScript is to use the string.slice() method. It is straightforward to use and takes less time compared to other approaches.

That’s it for this tutorial.

Related posts

How to Get Last Character from String in Javascript

How to Get First Character of String in JavaScript

How to Get Character from String in JavaScript

Leave a Comment