difference between shift and unshift in javascript
// Shift (remove) the first element of the array:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift(); //Orange,Apple,Mango
// Add new elements to an array:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon","Pineapple"); //Lemon,Pineapple,Banana,Orange,Apple,Mango
//The unshift() method adds one or more elements to the beginning of an array
//and returns the new length of the array.
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]
var name = [ "john" ];
name.unshift( "charlie" );
name.unshift( "joseph", "Jane" );
console.log(name);
//Output will be
[" joseph "," Jane ", " charlie ", " john "]
let array = ["A", "B"];
let variable = "what you want to add";
//Add the variable to the beginning of the array
array.unshift(variable);
//===========================
console.log(array);
//output =>
//["what you want to add" ,"A", "B"]
/*The unshift() adds method elements to the beginning, and push() method
adds elements to the end of an array.*/
let twentyThree = 'XXIII';
let romanNumerals = ['XXI', 'XXII'];
romanNumerals.unshift('XIX', 'XX');
// now equals ['XIX', 'XX', 'XXI', 'XXII']
romanNumerals.push(twentyThree);
// now equals ['XIX', 'XX', 'XXI', 'XXII', 'XXIII']
//The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon");
//The unshift() method returns the new array length : >> 5
/*if you find this answer is useful ,
upvote ⇑⇑ , so the others can benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)*/