const famousSayings = ['Fortune favors the brave.',
'A joke is a very serious thing.',
'Where there is love there is life.'];
const newYearsResolutions = ['Keep a journal', 'Take a falconry class'];
console.log(newYearsResolutions.length);
const itemTracker = ['item 0', 'item 1', 'item 2'];
itemTracker.push('item 3', 'item 4');
console.log(itemTracker);
// Output: ['item 0', 'item 1', 'item 2', 'item 3', 'item 4'];
const newItemTracker = ['item 0', 'item 1', 'item 2'];
const removed = newItemTracker.pop(); // remove the last item
console.log(newItemTracker);
// Output: [ 'item 0', 'item 1' ]
console.log(removed);
// Output: item 2
// shift remove the first item
const groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains'];
groceryList.shift(); // remove the first item
groceryList.unshift('popcorn'); // add item at the begining of the list
console.log(groceryList);
console.log(groceryList.slice(1,4)); // substract from list
console.log(groceryList);
const pastaIndex = groceryList.indexOf('pasta');
console.log(pastaIndex);
- Arrays are lists that store data in JavaScript.
- Arrays are created with brackets
[].
- Each item inside of an array is at a numbered position, or index, starting at
0.
- We can access one item in an array using its index, with syntax like:
myArray[0].
- We can also change an item in an array using its index, with syntax like
myArray[0] = 'new string';
- Arrays have a
length property, which allows you to see how many items are in an array.
- Arrays have their own methods, including
.push() and .pop(), which add and remove items from an array, respectively.
- Arrays have many methods that perform different tasks, such as
.slice() and .shift(), you can find documentation at the Mozilla Developer Network website.
- Some built-in methods are mutating, meaning the method will change the array, while others are not mutating. You can always check the documentation.
- Variables that contain arrays can be declared with
let or const. Even when declared with const, arrays are still mutable. However, a variable declared with const cannot be reassigned.
- Arrays mutated inside of a function will keep that change even outside the function.
- Arrays can be nested inside other arrays.
- To access elements in nested arrays chain indices using bracket notation.