How to use String.prototype.split() In Javascript

This method involves splitting the string into an array of characters using the split() method, then removing the first character by slicing the array from index 1 onwards, and finally joining the array back into a string.

Example:

JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Delete first character of a string in JavaScript</title>
</head>
<body>
    <h3>Delete first character of a string</h3>
    <p>Click the button to display the extracted part of the string</p>

    <button onclick="deleteFirstCharacter()">
        Click Here!
    </button>

    <p id="output"></p>

    <script>
        function deleteFirstCharacter() {
            let str = "example";
            let newStr = str.split('').slice(1).join('');
            console.log(newStr); // Output: "xample"
            document.getElementById("output").innerHTML = newStr;
        }
    </script>
</body>
</html>

Output:

Delete first character of a string in JavaScript

In this article, we will delete the first character of a string in Javascript. There are many ways to delete the first character of a string in JavaScript.

Below are the methods used to delete the first character of a string in JavaScript:

Table of Content

  • Using slice() Method
  • Using substr() Method
  • Using replace() method
  • Using the substring() method  
  • Using String.prototype.split()
  • Using Array shift()

Similar Reads

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

Using substr() Method

The string.substr() is an inbuilt function in JavaScript that is used to return the part of the given string from the start index to the end index. Indexing starts from zero (0)....

Using replace() method

The string.replace() function is an inbuilt function in JavaScript that is used to replace a part of the given string with some other string or a regular expression. The original string will remain unchanged....

Using the substring() method

In javascript the substring method returns a substring of string based on the index numbers provided as arguments....

Using String.prototype.split()

This method involves splitting the string into an array of characters using the split() method, then removing the first character by slicing the array from index 1 onwards, and finally joining the array back into a string....

Using Array shift()

The Array shift() approach converts the string to an array, removes the first element using shift(), and then joins it back to a string, effectively deleting the first character...