Shift / Unshift
All of these functions are responsible for either adding a item to an existing array, or removing an item from an existing array.
shift()¶
Remove and return the first item in an Array.
let arr = [1,2,3,4];
arr.shift(); // 1
arr; // [2,3,4]
unshift(value)¶
Add an item to the beginning of an Array and return the resulting length of the Array.
let arr = [1,2,3,4];
arr.unshift('kiwi'); // 5
arr; // ['kiwi',1,2,3,4]
Skill Drill¶
Create a
classmatesarray, that has the string representation of a few of your class' first names inside of it.Use
shift()to store the first name from yourclassmatesarray into a variable namedfirst.Use
console.logto print outfirst, use anotherconsole.logto print out theclassmatesarray.Use
unshift()to add the first name of three new people to yourclassmatesarray.Use
console.logto print out theclassmatesarray.