Day 12: Arrays & Objects

Arrays

Arrays are an ordered collection of values that are numerically indexed. Arrays are useful for storing values that are part of the same group, allowing us to perform operations on multiple values at once. Arrays have properties that we can access, and methods that allow us to interact with the array. Some common array methods include push (adds an element to the end of the array), pop (removes and stores the last element in the array), shift (remove the first element in the array), and unshift (add an element to the beginning of the array). We are able to select an element in the array by using its index, and we can also loop over arrays.

Array grocery example

Objects

Objects are a data structure that uses keys instead of an index to retrieve values. Keys are descriptive properties of objects, and we can use the key to store a value. If we want to access that value, we can simply reference the object and the key. Having keys instead of numeric indexes is more human-readable when we are working with our data. We can access our keys by either using Dot notation or Square bracket notation, and we can also update our values using the key. We can almost think of objects as real-world objects that also have properties, like a car has a make, model, and year, a car object would have these same properties.

Object with key-value pairs.

Closing

We are starting to move into more complex data structures. This will allow us to work more efficiently with data and keep our code DRY (Don’t Repeat Yourself) ✌️.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store