In this tutorial, we will learn about remove the first character from a string in JavaScript. In JavaScript, removing the first character involves eliminating the initial character to obtain a new string without the first character. This operation is commonly needed when manipulating strings in various programming tasks.
RELATED POST : JavaScript Capitalize First Letter
Remove the First Character from the String in JavaScript
We can remove the first character from a string in JavaScript using various methods. Here are a few approaches:
- Using substring()
- Using slice()
- Using substr()
- Using Regular Expression
By using substring() method
The substring() method in JavaScript is used to extract a portion of a string, starting from a specified index and extending up to the end of the string or another specified index. To remove the first character from a string using substring(), we need to specify the starting index as 1, which effectively excludes the first character from the resulting substring.
Syntax:
string.substring(start);
string: The original string from which the substring will be extracted.start: The index from which the extraction should begin. The character at this index will not be included in the resulting substring.
Example:
// Example of removing the first character from a string using substring()
function removeFirstCharacter(str) {
return str.substring(1);
}
const originalString = "Developerhelps";
const modifiedString = removeFirstCharacter(originalString);
console.log(modifiedString);
Output:
eveloperhelps
By using slice() method
The slice() method in JavaScript can be used to remove the first character from a string by extracting a portion of the original string starting from a specified index and returning the resulting substring.
Syntax:
const modifiedString = originalString.slice(startIndex);
originalStringis the string from which you want to remove the first character.startIndexis the index from which the extraction should begin. If thestartIndexis not provided, theslice()method starts from index 0 (the first character).
Example:
// Using slice() method to remove the first character from a string
const originalString = "Developerhelps";
const modifiedString = originalString.slice(1);
console.log(modifiedString);
Output:
eveloperhelps
By using substr() method
The substr() method in JavaScript is used to extract a portion of a string, starting from a specified position (index), and returning the desired number of characters. To remove the first character from a string, we can utilize the substr() method by specifying an index of 1, which effectively skips the first character and gives us the rest of the string.
Syntax:
string.substr(startIndex, length)
startIndex: The index (zero-based) from which the extraction should begin. To remove the first character, set it to 1.length: Optional. The number of characters to extract from the original string. If not specified,substr()will extract all characters from the starting index to the end of the string.
Example:
// Example of removing the first character from a string using substr()
function removeFirstCharacter(str) {
return str.substr(1);
}
const originalString = "Developerhelps";
const modifiedString = removeFirstCharacter(originalString);
console.log(modifiedString);
Output:
eveloperhelps
By using Regular Expression
The regular expression is a pattern used to match character combinations in strings. In JavaScript, regular expressions are defined between two forward slashes (/…/), and they can be used with the replace() method to modify strings based on matching patterns. Regular expressions provide powerful pattern-matching capabilities, and in this case, we can create a pattern that matches the first character and replace it with an empty string.
Syntax:
function removeFirstCharacter(str) {
return str.replace(/^./, '');
}
- The
replace()method is called on the input string (str) and takes two arguments. - The first argument is the regular expression pattern
^.. The^represents the beginning of the string, and.matches any character. - As a result, the regular expression
^.matches the first character of the string. - The second argument is an empty string
'', which effectively removes the matched first character. - The
replace()method replaces the first character that matches the pattern with an empty string, effectively removing it from the string.
Example:
const originalString = "Developerhelps";
const modifiedString = removeFirstCharacter(originalString);
console.log(modifiedString);
Output:
eveloperhelps












