xxxxxxxxxx
// Example array
let fruits = ["apple", "banana", "cherry"];
// Custom function to shift elements in the array
function shiftArray(arr) {
arr.shift();
return arr;
}
// Call the function and log the updated array
console.log(shiftArray(fruits));
xxxxxxxxxx
//The shift() method removes the first element from an array
//and returns that removed element.
//This method changes the length of the array.
const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1);
// expected output: Array [2, 3]
console.log(firstElement);
// expected output: 1
xxxxxxxxxx
let array = ["A", "B", "C"];
//Removes the first element of the array
array.shift();
//===========================
console.log(array);
//output =>
//["B", "C"]
xxxxxxxxxx
//Array.shift() removes the first array element and returns it value
//example 1
const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1);
// expected output: Array [2, 3]
console.log(firstElement);
// expected output: 1
//example 2
var arr = ["f", "o", "o", "b", "a", "r"];
arr.shift();
console.log(arr); // ["o", "o", "b", "a", "r"]
xxxxxxxxxx
let largeCountries = ["Tuvalu","India","USA","Indonesia","Monaco"]
// Use push() & pop() and their counterparts unshift() & shift()
largeCountries.pop()
largeCountries.push("Pakistan")
largeCountries.shift()
largeCountries.unshift("China")
//output
console.log(largeCountries)
["China", "India", "USA", "Indonesia", "Pakistan"]
xxxxxxxxxx
let myArray = [1, 2, 3, 4, 5];
let removedElement = myArray.shift();
console.log(removedElement); // Output: 1
console.log(myArray); // Output: [2, 3, 4, 5]
xxxxxxxxxx
let fruits = ["apple", "banana", "cherry"];
// Using the shift() method to remove the first element of the array
let shiftedFruit = fruits.shift();
console.log(shiftedFruit); // Outputs: "apple"
console.log(fruits); // Outputs: ["banana", "cherry"]