Delete first character of a string in JavaScript - GeeksforGeeks

07 May, 2019
There are many ways to delete the first character of a string in JavaScript, some of them are discussed below:
Method 1: Using slice() Method: The slice() method extracts the part of a string and returns the extracted part in a new string. If we want to remove the first character of a string then it can be done by specifying the start index from which the string needs to be extracted. We can also slice the last element.
Syntax:
string.slice(startingindex, endingindex)
Example:
<!DOCTYPE html><html>  <head>    <title>        Delete first character        of a string    </title></head>  <body>      <p>        Click on button to display the        extracted part of the string    </p>          <button onclick="myGeeks()">        Click Here!    </button>          <p id="GFG"></p>          <script>        function myGeeks() {            var str = "GeeksforGeeks";             var result = str.slice(1);                          document.getElementById("GFG").innerHTML                    = result;        }    </script></body>  </html>                    
Output:Before Clicking the button:After Clicking the button:
Method 2: Using substr() Method: The string.substring() is an inbuilt function in JavaScript which is used to return the part of the given string from start index to end index. Indexing starts from zero (0).
Syntax:
string.substring(Startindex, Endindex)
Example:
<!DOCTYPE html><html>  <head>    <title>        Delete first character        of a string    </title></head>  <body>      <p>        Click on button to display the        extracted part of the string    </p>          <button onclick="myGeeks()">        Click Here!    </button>          <p id="GFG"></p>      <script>        function myGeeks() {            var str = "GeeksforGeeks";            var result = str.substr(1);            document.getElementById("GFG").innerHTML                    = result;        }    </script></body>  </html>                    
Output:Before Clicking the button:After Clicking the button:
Method 3: Using replace() method: The string.replace() function is an inbuilt function in JavaScript which is used to replace a part of the given string with some other string or a regular expression. The original string will remain unchanged.
Syntax:
string.replace(searchvalue, newvalue)
Example:
<!DOCTYPE html><html>  <head>    <title>        Delete first character        of a string    </title></head>  <body>      <p>        Click on button to display the        extracted part of the string    </p>          <button onclick="myGeeks()">        Click Here!    </button>          <p id="GFG">GeeksforGeeks</p>      <script>        function myGeeks() {            var str = document.getElementById("GFG").innerHTML;             var res = str.replace("GeeksforGeeks", "eeksforGeeks");            document.getElementById("GFG").innerHTML = res;        }    </script></body>  </html>                    
Output:Before Clicking the button:After Clicking the button:
javascript-string
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?