Interactive JavaScript Arrays

By Van Warren, former IFSC 3300 student
fruits = []
Press CREATE to populate this JavaScript array.

fruits[0]
Press GET FIRST ELEMENT to access it.

fruits[fruits.length-1]
Press GET LAST ELEMENT to access it.

fruits.forEach(fruitFunction);
Press LOOP FOR EACH to loop on fruitFunction().

for(let i = 0; i < fruits.length; i++)
Press LOOP FOR INDEX to index over fruits array.

fruit.push('pear') appends 'pear' to the fruits array.
fruit.push('pear') returns the length of fruits array.
Press PUSH ITEM to add 'pear' to END of fruits array.

fruits.pop() removes item from the end of an array.
fruits.pop() returns the item shifted.
Press POP ITEM to remove 'pear' from END of fruits array.

fruits.unshift('mango') adds item to beginning of array.
fruits.unshift('mango') returns length of fruits array.
Press UNSHIFT ITEM to add 'mango' to START of fruits array.

fruits.shift() removes item from beginning of array.
fruits.shift() returns the item shifted.
Press SHIFT ITEM to remove 'mango' from START of fruits array.

fruits.indexOf('banana')
Press INDEX OF ITEM to find index of 'banana'.

fruits.splice(2, 1)
Press SPLICE OUT ITEM to splice out 'orange'.

newFruits = fruits.slice()
Press COPY ARRAY to copy fruits to newFruits.