Arrays. In. JS.

Arrays.
In.
JS.

A quick google search of the word 'Array' would yield two primary definitions:

  • 'an impressive display or range of a particular type of thing'

  • 'an ordered series or arrangement'

Both definitions are functionally correct with respect to Arrays in JavaScript, however not equally. In this piece, I will be discussing about Arrays in JavaScript as succinctly as I can.

Arrays in JavaScript are 'non-primitive' data types used to store a list of values.

They can be stored in Variables:

   let volkswagenGroup = ["Audi", "Lamborghini", "Bentley", "Porsche", "Ducati"];

Each value in an Array is referred to as an Element

So when should Arrays be used?

Arrays are most useful when working on a collection of values of the same data type or values that are related to one and they have limitless use cases such that they can store any data type including objects and EVEN other Arrays.

Thereafter, how can these Values be accessed?

To understand how they are accessed, it's important to understand the inner-workings of an Array. Every element in an array is assigned a number called an index. So by default, the first index of an array is zero. To put this into context, consider the example I Illustrated above.

  • Audi would have an Index of 0

  • Lamborghini would have an Index of 1

  • Bentley would have an Index of 2

As such, values in an array can be accessed and altered by using their index.

Accessing Values In Array:

volkswagenGroup[0]

This will print out 'Audi' if logged to the console.

volkswagenGroup[2]

This will print out 'Bentley' if logged to the console.

Altering Values in Array:

volkswagenGroup[3] = 'Mercedes-Benz'

This changes the element at Index number 3 which was originally Porsche to Mercedes-Benz

This basic foundational knowledge of Arrays in JavaScript is just the tip of the Iceberg. Arrays are VAST. They have a plethora of properties that can be used on them. For the purpose of this article, I won't be extending that further. However, should you wish to explore more on Arrays in JavaScript, here are some helpful resources:

w3schools

MDN

For some engaging exercises on Arrays: Turing