Unlock your Web Development skills with free daily content! 🚀

Home » Blog » 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 a 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 a 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 a string using Replace

In this 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.

Remove the first character from a string using Shift

The last part will discover how to take advantage of the array conversion to remove the first character from a string. We will convert the string into an array, use the built-in function Shift, and then convert the array back to a string.

const helloWorld = 'Hello world!'

// We split each character from the string
const helloWorldArray = helloWorld.split('')

helloWorldArray.shift()

// We convert back the array to a string
const newHelloWorld = helloWorldArray.join('')

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

What’s next?

It’s over! 🎉 Now you know how to remove the first character from a string in JavaScript, discover my other article about how to remove the last character from a string in JavaScript.


Thanks for reading. Let’s connect!

➡️ I help you grow into Web Development, and I share my journey as a Nomad Software Engineer. Join me on Twitter for more. 🚀🎒

Gaël Thomas

Unlock your Web Development skills! 🚀 I'm a Remote Software Engineer sharing educational content. Let's learn & grow together! 📚 DMs are open, let's connect (on Twitter) 🤝

Post navigation