UnShift Array | Add Element to Start of Array | JavaScript


Add Element to Beginning of Array or UnShift Array using JavaScript.

You can use JavaScript array unshift method to add element to the beginning of the array.

let arr = [1,2,3,4,5];
let modified_array_len = arr.unshift(6);
console.log('Modified array is ', arr);
console.log('Modifed array len is ', modified_array_len);

// -   "Modified array is ", [6, 1, 2, 3, 4, 5]
// -   "Modifed array len is ", 6

unshift method moves elements to the right of the array after adding new elements to start of the array. You can also add multiple elements separated by comma.

let arr = [1,2,3,4,5];
arr.unshift(7,8,9);
console.log('Modified array is ', arr);
// "Modified array is ",  [7,  8,  9,  1,  2,  3,  4,  5]