1var name = [ "john" ];
2name.unshift( "charlie" );
3name.unshift( "joseph", "Jane" );
4console.log(name);
5
6//Output will be
7[" joseph "," Jane ", " charlie ", " john "]
1// Use unshift method if you don't mind mutating issue
2// If you want to avoid mutating issue
3const array = [3, 2, 1]
4
5const newFirstElement = 4
6
7const newArray = [newFirstElement].concat(array) // [ 4, 3, 2, 1 ]
8
9console.log(newArray);
10
1let cats = ['Bob', 'Willy', 'Mini'];
2
3cats.shift(); // ['Willy', 'Mini']
4
5let cats = ['Bob'];
6
7cats.unshift('Willy'); // ['Willy', 'Bob']
8
9cats.unshift('Puff', 'George'); // ['Puff', 'George', 'Willy', 'Bob']
1/*The unshift() adds method elements to the beginning, and push() method
2adds elements to the end of an array.*/
3let twentyThree = 'XXIII';
4let romanNumerals = ['XXI', 'XXII'];
5
6romanNumerals.unshift('XIX', 'XX');
7// now equals ['XIX', 'XX', 'XXI', 'XXII']
8
9romanNumerals.push(twentyThree);
10// now equals ['XIX', 'XX', 'XXI', 'XXII', 'XXIII']
1//My Opinion is really nothing. They are the same exept one adds a element
2//to the end and the other one adds a element to the front
3// Push Method
4var array = [2];//your random array
5array.push(3)
6// Unshift Method
7array.unshift(1)