Array.push, pop, unshift, shift Flashcards
The________ method adds one or more elements to the end of an array and returns the new length of the array.
push()
var animals = [‘pigs’, ‘goats’, ‘sheep’];
console.log(animals.push('cows')); // expected output:
console.log(animals); // expected output:
4
Array [“pigs”, “goats”, “sheep”, “cows”]
both elements that add to array
push , unshift
The___________method removes the last element from an array and returns that element. This method changes the length of the array.
pop()
var plants = [‘broccoli’, ‘cauliflower’, ‘cabbage’, ‘kale’, ‘tomato’];
console.log(\_\_\_\_\_\_\_\_\_); // expected output: "tomato"
plants.pop()
var plants = [‘broccoli’, ‘cauliflower’, ‘cabbage’, ‘kale’, ‘tomato’];
console. log(plants.pop());
console. log(plants);
// expected output: “tomato”
// expected output: Array [“broccoli”, “cauliflower”, “cabbage”, “kale”]
var array1 = [1, 2, 3];
console. log(array1.pop());
console. log(array1);
3
[1,2]
The _________method adds one or more elements to the beginning of an array and returns the new length of the array.
unshift()
var array1 = [1, 2, 3];
console.log(array1.unshift(4, 5)); // expected output:
5 returns length
The ___________ method removes the first element from an array and returns that removed element. This method changes the length of the array.
shift()
var array1 = [1, 2, 3];
var firstElement = _________
console.log(array1); // expected output: Array [2, 3]
console.log(firstElement); // expected output: 1
array1.shift();
shift()
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
The orderQueue array contains a list of customer orders. Create a new variable named shipping – remove the first item from the array and place it in the shipping variable
var orderQueue = ['1XT567437','1U7857317','1I9222528']; var shipping =
orderQueue.shift();
The _________ method removes the first element from an array and returns that removed element. This method changes the length of the array.
shift()
var array1 = [1, 2, 3];
var firstElement = array1.shift();
console.log(array1); // expected output:
console.log(firstElement); // expected output:
Array [2, 3]
1