JavaScript Fundamentals -- Arrays in JavaScript

Creating and Working with Arrays


Let's start by learning to create arrays. To create an array, you wrap a comma separated list of items in square brackets. The items can be strings, numbers, objects, booleans, etc. It can even be a mix, although that's not exactly recommended.

const dogs = ['Duke', 'Max', 'Cincy'];
const ages = [1, 2, 3, 4];
const people = [{ name: 'Preston' }, { name: 'Amanda' }];
const mix = ['Duke', 3, { name: 'Preston' }];

To add an item to an array, use the push method. The argument you pass in to the method will be added on to the end of the array:

console.log(dogs); // [ 'Duke', 'Max', 'Cincy', 'Apollo' ]

If you need to add something to the beginning (front) of the array, use the unshift method:

console.log(ages); // [0, 1, 2, 3, 4]

I finished! On to the next chapter