JS Array is a subtype of Object

21 Aug 2020

Try below code first:

'a' in ['a', 'b', 'c']
// output: false

0 in ['b', 'b', 'c']
// output: true

You may ask why? Let’s see the in’s documentation:

The in operator returns true if the specified property is in the specified object or its prototype chain.

So, the in is looking for key not value. But what’s the key/value in an array.

'a' in { 0: 'a', 1: 'b', 2: 'c' }
// output: false

0 in { 0: 'a', 1: 'b', 2: 'c' }
// output: false

So in JS, you can image the array just as a subtype of Object.

Back to top