How to Remove the First Character From String in JavaScript

Kirill Ibrahim Feb 02, 2024
  1. JavaScript substring() Method to Remove the First Character From String
  2. JavaScript slice() Method to Remove the First Character From String
  3. JavaScript replace() Method to Remove the First Character From String
How to Remove the First Character From String in JavaScript

JavaScript has different methods to remove the first character from a string. Since strings are immutable in JavaScript, so the idea is to create a new string. Every method below will have a code example, which you can run on your machine.

JavaScript substring() Method to Remove the First Character From String

The substring() function is a built-in function in JavaScript. It returns new a string from the start index to the end index of a given string.

Syntax of substring

substring(startIndex, endIndex)

The startIndex is required, and endIndex is optional. If endIndex is not specified, substring() selects all characters from the startIndex to the string’s end.

Example:

<!DOCTYPE html> 
<html> 
<head> 
    <title> 
        Remove the first character 
    </title> 
</head> 
  
<body> 
    <h2> 
        Click on button to display the 
        `DelftStack` without first character.
    </h2> 
      
    <button onclick="removeFirstChar()"> 
        Click Button
    </button> 
      
    <p id="displayString"></p> 
  
    <script>         
        const removeFirstChar = () => {
             let str1 = "DelftStack";
            let str2 = str1.substr(1);
            console.log(str2);
            document.getElementById("displayString").innerHTML  = str2;      
        }
    </script> 
</body>   

</html>

By passing 1 as the parameter, the function will return everything from index 1 and onward.

JavaScript slice() Method to Remove the First Character From String

The slice() method extracts the part of the string and returns that part in a new string.

Syntax of the slice() Method

slice(startIndex, endIndex)

The startIndex is required, and endIndex is optional. If endIndex is not specified, slice() selects all characters from the startIndex to the end of the string.

Example:

<!DOCTYPE html> 
<html> 
<head> 
    <title> 
        Remove the first character 
    </title> 
</head> 
  
<body> 
  
    <h2> 
        Click on button to display the 
        `DelftStack` without first character.
    </h2> 
      
    <button onclick="removeFirstChar()"> 
        Click Button
    </button> 
      
    <p id="displayString"></p> 
  
    <script>         
        const removeFirstChar = () => {
             let str1 = "DelftStack";
            let str2 = str1.slice(1);
            console.log(str2);
            document.getElementById("displayString").innerHTML  = str2;      
        }
    </script> 
</body> 
  
</html>

JavaScript replace() Method to Remove the First Character From String

The replace() method is used to replace a part of a string with a new replacement string.

Syntax of the replace Method

replace(paramA, paramB)

The paramA is a specified string or regular expression from a part of the given string that we want to replace with a new value, paramB is a new value. Both are required.

Here, we use replace(/^./, "") to remove the first character because /^./ means the first character and "" is the empty string.

Example:

<!DOCTYPE html> 
<html> 
<head> 
    <title> 
        Remove the first character 
    </title> 
</head> 
  
<body> 
  
    <h2> 
        Click on button to display the 
        `DelftStack` without first character.
    </h2> 
      
    <button onclick="removeFirstChar()"> 
        Click Button
    </button> 
      
    <p id="displayString">DelftStack</p> 
  
    <script>         
        const removeFirstChar = () => {
            let str1 = document.getElementById("displayString").innerHTML;  
            let str2 = str1.replace(/^./, ""); 
            document.getElementById("displayString").innerHTML = str2;
            console.log(str2);     
        }
    </script> 
</body> 
  
</html>

Related Article - JavaScript String