Arrays are containers that can store multiple values. Here is one that stores 3 elements of different types:

const myArray = ['a', 42, 3.14];

`Arrays` are implemented in JavaScript as objects:

console.log(typeof myArray);
 object
console.log(Array.isArray(myArray));
 true

Add & remove elements

2 methods are available to add and remove elements from the end of an array: `pop` & `push`.

`push` allows one to add one or more elements at the end of the array - in the example below we're adding a string and an object:

myArray.push('Hello', {'x': 1});
 5   // returns the new number of elements in our array
console.log(myArray); 
 Array(5) [ "a", 42, 3.14, "Hello", {…} ]

`pop` removes and returns the last element:

myArray.pop(); // Object { x: 1 }
console.log(myArray); // Array(4) [ "a", 42, 3.14, "Hello" ]

To add and remove elements from the beginning of the array, we'll use `unshift` and `shift`.

Just like `push`, `unshift` takes one or more parameters:

myArray.unshift('Start', {'y': 2}); // 6
console.log(myArray); // Array(6) [ "Start", {…}, "a", 42, 3.14, "Hello" ]

In a similar fashion, `shift` removes and returns the first element:

myArray.shift(); // "Start"
console.log(myArray); // Array(5) [ {…}, "a", 42, 3.14, "Hello" ]

Element indexes

Arrays are 0-indexed, so to access the 3rd element of the array we'll use:

myArray[2]; // 42

Indexes are also used if one wants to extract a range of elements from the array - in the example below we're extracting elements from index 2 to index 4. Notice that index 4 is not included in the response:

myArray.slice(2, 4); // Array [ 42, 3.14 ]

Iteration thought the elements

There are several wayt to iterate though the elements of an array.

Using `forEach`:

myArray.forEach(function(item) {
	console.log(item);
});

The classic `for` loop:

for(let i = 0; i < myArray.length; i++) { 
  console.log(myArray[i]);
}

`for ... in` loop:

for(const item of myArray) {
  console.log(item);
};

Next Steps