JavaScript Delete First Character of a String (original) (raw)

Last Updated : 16 Nov, 2024

To delete the first character of a string in JavaScript, you can use several methods. Here are some of the most common ones

**Using slice()

The slice() method is frequently used to remove the first character by returning a new string from index 1 to the end.

JavaScript `

let s1 = "GeeksforGeeks"; let s2 = s1.slice(1); console.log(s2);

`

**Output

eeksforGeeks

Using substring()

Similar to slice(), substring() creates a new string starting from index 1, effectively removing the first character.

JavaScript `

let str = "GeeksforGeeks"; let newStr = str.substring(1); console.log(newStr);

`

**Output

eeksforGeeks

Using Array Destructuring with join()

Convert the string into an array of characters, remove the first character, and join it back into a string.

JavaScript `

let s1 = "GeeksforGeeks"; let s2 = [...s1].slice(1).join(''); console.log(s2);

`

**Output

eeksforGeeks

Using replace() with Regular Expression

The replace() method removes the first character using a regular expression that matches the first character (^.).

JavaScript `

let s1 = "GeeksforGeeks"; let s2 = s1.replace(/^./, ''); console.log(s2);

`

**Output

eeksforGeeks

**Note: In most cases, slice() and substring() are preferred for their simplicity and readability.