JavaScript - unshift() method

revision:


Category : array

The unshift() method adds the specified elements to the beginning of an array and returns the new length of the array.

Syntax :

        unshift()
        unshift(element0)
        unshift(element0, element1)
        unshift(element0, element1, /* … ,*/ elementN)
    

Parameters:

elementN : the elements to add to the front of the "arr".

Examples:

            const array1 = [1, 2, 3];
            console.log(array1.unshift(4, 5));
            // Expected output: 5
            console.log(array1);
            // Expected output: Array [4, 5, 1, 2, 3]
        

Practical examples

example: using unshift() method.

code:
                    <div>
                        <p id="unshift-1"> </p>
                        <p id="unshift-2"> </p>
                        <p id="unshift-3"> </p>
                        <p id="unshift-4"> </p>
                        <p id="unshift-5"> </p>
                        <p id="unshift-6"> </p>
                    </div>
                    <script>
                        const arr = [1, 2];
                        document.getElementById("unshift-1").innerHTML = "original array : " + arr;
                        arr.unshift(0); // result of the call is 3, which is the new array length
                        // arr is [0, 1, 2]
                        document.getElementById("unshift-2").innerHTML = "unshift 0 : " + arr;
                        arr.unshift(-2, -1); // the new array length is 5
                        // arr is [-2, -1, 0, 1, 2]
                        document.getElementById("unshift-3").innerHTML = "unshift -2 -1 : " + arr;
                        arr.unshift([-4, -3]); // the new array length is 6
                        // arr is [[-4, -3], -2, -1, 0, 1, 2]
                        document.getElementById("unshift-4").innerHTML = "unshift -4 -3 : " + arr;
                        arr.unshift([-7, -6], [-5]); // the new array length is 8
                        // arr is [ [-7, -6], [-5], [-4, -3], -2, -1, 0, 1, 2 ]
                        document.getElementById("unshift-5").innerHTML = "unshift [-7 -6], [-5] : " + arr;
                        document.getElementById("unshift-6").innerHTML = "unshift : " + arr.length;
                        
                    </script>
                

example: add new elements to an array

code:
                    <div>
                        <p id="unshift-7"></p>
                        <p id="unshift-8"></p>
                        <p id="unshift-9"></p>
                    </div>
                    <script>
                        const fruits = [" Banana", " Orange", " Apple", " Mango"];
                        document.getElementById("unshift-7").innerHTML = "array : " + fruits
                        fruits.unshift(" Lemon", " Pineapple");
                        document.getElementById("unshift-8").innerHTML = "new array : " + fruits;
                        document.getElementById("unshift-9").innerHTML = "array length: " + fruits.unshift();
                        
                    </script>