The blog of David Wickes, software developer

Arrays in JavaScript

Arrays are easy, right? Pretty basic go-to data structure. Pretty primative. So if we did something like this - we wouldn’t be messing with the array would we?

array = ["tom", "alan", "harry"]
array.suprise = "dave"

surely not?

for (index in array) { console.log(index) }

//=> 0
//=> 1
//=> 2
//=> surprise

//=> ["tom", "alan", "harry", suprise: "dave"]

Which might just be a good reason not to use for... in, or trusting it to put things as strings in a sensible way, but is a good demonstration of…

typeof bob
//=> "object"

Because everything is an object in JS, the interface for arrays is kinda hacked together out of object properties. Arrays are built on top of objects, they are not a simpler data-type, and objects in JS are just collections of strings that point at things.

(and there aren’t just arrays to consider - when there’s the world of Array-like objects to look at. But more on that another time.)