Remove First Character from a String in JavaScript

Remove First Character from a String in JavaScript

A short tutorial on how to get and remove the first character of string in JavaScript.

Remove the first character from String using Substring

The most common way is by using the JavaScript substring method. This method can take up to two indexes as parameters and allow you to get the string between these two values.

If you want to remove the first character of a string, you will only need the first parameter. When the function is called with only one parameter, you get the characters between the first index and the string's end.

const helloWorld = 'Hello world!'
const newHelloWorld = helloWorld.substring(1) // 1 = index of "e"

console.log(newHelloWorld)
// Output: "ello world!"

Now that you know how to remove the first character from a string, you can move the index to remove more characters.

const helloWorld = 'Hello world!'
const newHelloWorld = helloWorld.substring(6) // 6 = index of "w"

console.log(newHelloWorld)
// Output: "world!"

If you want to have more information, you can read the subtring documentation.

Remove the first character from String using Slice

Another way to remove the first character is by using the JavaScript slice method. In this case, this function is similar to the substring method. You can send an index as the first parameter, and the function will return your string without the first character.

const helloWorld = 'Hello world!'
const newHelloWorld = helloWorld.slice(1)

console.log(newHelloWorld)
// Output: "ello world!"

As the previous example, now that you know how to remove the first character from a string, you can move the index to remove more characters.

const helloWorld = 'Hello world!'
const newHelloWorld = helloWorld.slice(6)

console.log(newHelloWorld)
// Output: "world!"

If you want to have more information, you can read the slice documentation.

Get and remove the first character from String using Replace

In this last part, you will use the JavaScript replace method combined with the charAt method.

The chatAt function allows you to find the character at the first position. It's similar to using helloWorld[0]. The only difference is the return value if the index doesn't exist.

const helloWorld = ''

console.log(helloWorld[0]) // Output: undefined

console.log(helloWorld.charAt(0)) // Output: "" (empty string)

The replace function allows replacing occurrences of a pattern with another value.

One important thing to know, replace will replace only the first occurrence of a value if the pattern (first parameter) is a string.

In the example below, you will find the first character of a string and then replace the first occurrence of it.

const helloWorld = 'Hello world! Hello!'

const firstChar = helloWorld.charAt(0)
console.log(firstChar)
// Output: "H"

const newHelloWorld = helloWorld.replace(firstChar, '') // Replace only the first occurence of "H"

console.log(newHelloWorld)
// Output: "ello world! Hello!"

If you want to have more information, you can read the replace documentation.


Thank you for reading, and I hope you found this helpful! Follow me on Twitter, where I tweet about web development, self-improvement, and my journey as a fullstack developer!

Subscribe to my newsletter.

By subscribing, you will receive the latest articles, courses and tutorials available.

Learn to code with simple and concrete examples.

English / Français