How to Remove First Character from String in JavaScript

Here are three ways to remove the first character from a string in JavaScript.

  1. Using slice()
  2. Using substring()
  3. Using replace()

Method 1: Using the slice()

To remove the first character from the string in JavaScript, you can use the “slice()” method. The string.slice() is a built-in method that extracts a part of a string. It does not change the original string.

Syntax

string.slice(startIndex, endIndex);

Example

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

Output

reetings from JavaScript

Method 2: Using the substring() function

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, by default, it will consider the first parameter as 0.

Syntax

string.substring(startIndex, endIndex);

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.

Don’t use the “substr()” method to remove the first character from a string in JavaScript because it is deprecated and won’t be available in the future. You can learn more about this on Mozilla docs.

Method 3: Using the 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)

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 efficient way to remove the first character from a string in JavaScript is to use the string.slice() method.

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