Practice with Arrays

by Ken Sprague

JavaScript

//open your console and learn how to manipulate an array. 
//assigned console.log to variable log.
var log = console.log;

// creating an array
var comics = ["marvel", "dc", "image", "valiant"];

//keep this log uncommented to see the base array.
log("Before: ", comics);

//uncomment each log to dive deeper into learning more about array properties and methods. Changes can be seen in the console. 

// 1.returns the number of elements in the array:
// log("Array length: ", comics.length);

// 2.loop over an array
// comics.forEach(function(item, index, array){
// log(index, item);
// });

// 3.reverse the array:
// log("After: ", comics.reverse());

// 4.remove the first value of the array:
// log("After: ", comics.shift());

// 5.add comma-separated list of values to the front of the array:
// log("After: ", comics.unshift("dark horse", "harris"));

// 6.remove the last value of the array:
// log("After: ", comics.pop());

// 7.add comma-separated list of values to the end of the array:
// log("After: ", comics.push("harris", "dark horse"));

// 8.find the specified position (pos) and remove n number of items from the array.
// log("After: ", comics.splice(1, 2)); // Starts at given index item and removes number of items from starting point.

// 9.create a copy of an array. typically assigned to a new variable:
// var newComics = comics.slice();
// log("After: ", "New comics: ", newComics);

// MDN documentation for Array:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array