Ads by ProfitSence
Close

Delete the First Character JavaScript

Last Updated on Wednesday 5th Oct 2022

Let’s assume you have a string, and you need to remove the first character from string or first two characters from that string JavaScript.

			
					let str = "Front-End Web Development"

			
	

Want to remove from the first F letter of that str.

  • slice() won't modify the original array
  • slice() method extracts parts of a string
			
					let result = str.slice(1);
console.log(result);
/*
  Output: ront-End Web Development
*/

			
	

Remove the first character from String using Substring

JavaScript substring is the most popular method.

  • This method allows you to obtain the string between two indexes by taking up to two parameters.
  • If you want to remove the first character, you will only need the first parameter.

  • When the function is called only one parameter, you get the characters between the first index and the string's end.

			
					const grateFul = 'We are grateful for your visit!'
const newThing = grateFul.substring(1) // 1 = index of "e"

console.log(newThing)
// Output: "e are grateful for your visit!"

			
	

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

			
					const grateFul = 'We are grateful for your visit!'
const newThing = grateFul.substring(5)

console.log(newThing)
// Output: "e grateful for your visit!"

			
	

You Can Read the subString Documentation

Get and remove the first character from String using Replace

You will use the JavaScript replace method combined with the charAt method.

The charAt function allows you

  • To find the character at the first position.
  • It's similar to using grateFul[0].
  • The only difference is the return value if the index doesn't exist.
			
					const grateFul = 'We are grateful'
const getFirst = grateFul.charAt(0)

console.log(getFirst)
// Output: "W"

const newThing = grateFul.replace(getFirst, '')

console.log(newThing)
// e are grateful

			
	

You Can Read the replace Documentation