Arrays

For absolute beginners.

What is an array?

An array is a collection of data grouped and stored in a contiguous block of memory. An array can be made of any data type: string, boolean, number, an array of arrays, etc.

Advantages of arrays

The main advantage of arrays is the ability to look up any value in an array based on its index with O(1) time complexity.

const array = ["a", "b", "c"]// The value at array[0] is "a"
// The value at array[1] is "b"
// The value at array[2] is "c"

NOTE: indexing starts at 0.

Disadvantages of arrays

Elements cannot be added to an array when that original, fixed array is full.

ENTER: Dynamically Resizing Array

A dynamically resizing array is still an array in all the ways described above; however, when you append to a full array, a new array of double the original array size is created, i.e., if the original array was 2 elements long, the new array will be 4 elements in size.

The data from the original, full array will be copied to the new array. The new value (the triggered all this nonsense) will be appended to the new, longer array. The original array is deleted.

Now that I’ve used the word array more times than anyone has a right to do…wtf are they used for?

Useful applications of arrays

  • Storing data that belong together.
  • Storing ordered data.
  • Storing data to refer back to later.

Full-stack developer. Funny-peculiar. Lover of baking, yoga, and reading (between the lines).