How to Delete the First Character of a String in JavaScript

You can use a bunch of JavaScript methods to remove the first character of a string. Let’s see what they are and choose the one suited for your case.

slice()

The slice() method extracts the section of a string and returns the extracted part in a brand new string. If you want to delete the first character of a string then you should specify the start index from which the string needs to be extracted:

Javascript slice method
let str = 'W3docs'; str = str.slice(1); console.log(str);// Output: 3docs

The slice() method can be used to delete first N characters from the string:

Javascript slice method
let str = 'W3docs'; let n = 2; str = str.slice(n); console.log(str); // Output: docs

substring()

The substring() is a built-in function in JavaScript which is used to return the part of the given string from starting index to end index. Indexing starts from zero (0):

Javascript substring method
let str = 'W3docs'; str = str.substring(1); console.log(str); // Output: 3docs

The substing() method can also be used to remove first N characters from the string:

Javascript substring method
let str = 'W3docs'; let n = 4; str = str.substring(n); console.log(str); // Output: cs

substr()

The substr() method extracts the part of a string, starting at the character at the specified position, and returns the specified N of characters:

Javascript substr method
let str = 'W3docs'; str = str.substr(1); console.log(str); // Output: 3docs
The substr() method does not modify the original string.

The substr() and substring() methods differ. The substr() method can accept a negative starting position as an offset from the end of the string, while the substring does not. However, the substr() method may be deprecated in future.