JS: unshift() Method

Sometimes you must push an element at the front of the array. This is not possible with the regular push() method of the array class.

Fortunately, JavaScript has a function called unshift() that push the elements at the front of the array.

The shift method can be used to push

  1. single element
  2. two or more element
  3. a sub array

In this example, we will try to push a single element and a sub-array to a JavaScript array.

Consider the following example.

var myArray = new Array(23,44,5.5,'tomato');

The output of the above script is given below.


Please support us by disabling your adblocker or whitelist this site from your adblocker. Thanks!

turn of adblocker imag