Sometimes you must push an element at the front of the array. This is not possible with the regular
<span style="color:#cf2e2e" class="tadv-color">push()</span> method of the array class.
<span style="color:#cf2e2e" class="tadv-color"> unshift()</span> that push the elements at the front of the array.
The shift method can be used to push
- single element
- two or more element
- a sub array
Consider the following example.
var myArray = new Array(23,44,5.5,'tomato'); myArray.unshift("pen"); myArray.unshift([23,66]); console.log(myArray)
The output of the above script is given below.