Arrays are lists of ordered, stored data. They can hold items that are of any data type. Arrays are created by using square brackets, with individual elements separated by commas.
Array elements are arranged by index values, starting at
0 as the first element index. Elements can be accessed by their index using the array name, and the index surrounded by square brackets.
.push() mutates the original array returns the new length of the array.
.pop() method removes the last element from an array and returns that element.
Even if they are declared using
const, the contents can be manipulated by reassigning internal values or using methods like
- 1Organizing and storing data is a foundational concept of programming. One way we organize data in real life is by making lists. Let’s make one here: New Year’s Resolutions: 1. Keep a journal 2…
- 2One way we can create an array is to use an array literal. An array literal creates an array by wrapping items in square brackets . Remember from the previous exercise, arrays can store any da…
- 3Each element in an array has a numbered position known as its index. We can access individual items using their index, which is similar to referencing an item in a list based on the item’s positi…
- 4In the previous exercise, you learned how to access elements inside an array or a string by using an index. Once you have access to an element in an array, you can update its value. let seasons =…
- 5You may recall that you can declare variables with both the let and const keywords. Variables declared with let can be reassigned. Variables declared with the const keyword cannot be reassigned. …
- 6One of an array’s built-in properties is length and it returns the number of items in the array. We access the .length property just like we do with strings. Check the example below: const newYear…
- 8Another array method, .pop(), removes the last item of an array. const newItemTracker = [‘item 0’, ‘item 1’, ‘item 2’]; const removed = newItemTracker.pop(); console.log(newItemTracker); // Ou…
- 9There are many more array methods than just .push() and .pop(). You can read about all of the array methods that exist on the Mozilla Developer Network (MDN) array documentation . .pop() and .p…
- 10Throughout the lesson we went over arrays being mutable, or changeable. Well what happens if we try to change an array inside a function? Does the array keep the change after the function call or i…
- 11Earlier we mentioned that arrays can store other arrays. When an array contains another array it is known as a nested array. Examine the example below: const nestedArr = [, [2, 3]]; To acce…