One Strange Thing About Arrays In JavaScript

Kaung Myat Lwin
Jun 28, 2021

--

One gotcha in this strange language of ours is: Everything could be considered as “objects”.

[1, 2, 3] array is considered as an “object”, and it means we could, technically use Object.prototype methods with it. So here’s the result; the output being INDEX as a KEY, VALUE as VALUE.

You could consider an array as key-value pair in this way. However “typeof null” might output “object” as it’s type (it is an unfixed bug in JavaScript engine written from long ago and they didn’t bother fixing it because it’s way too strangled), couldn’t be used with most Object.prototype methods. It will throw a “Reference Error”.

Interesting take. This could also be used to tease a candidate for JS interview questions. However one shouldn’t be very serious of this question as it doesn’t even matter in actual production stages.

--

--