# JS Tip of the Day: Is it an Array?

**URL:** <https://forum.kirupa.com/t/js-tip-of-the-day-is-it-an-array/643155>\
**Category:** web dev\
**Created:** [March 12, 2020, 4:56pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-is-it-an-array/643155 "2020-03-12T16:56:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 12, 2020, 4:56pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-is-it-an-array/643155/1 "2020-03-12T16:56:10Z")

</div>

**Is it an Array?**  
Level: Beginner

Sometimes you need an array. And if you ever need to know if some arbitrary value is an array, you should know how to accurately test for that.

We’ve seen how `typeof` can be used to look at value types, but it is largely only useful for primitives. When used on arrays, arrays being objects, `typeof` will return `"object"`. There’s also an `instanceof` operator but this too has its limitations, looking only at inheritance chains and not recognizing types from other global spaces, for example from other frames or windows in browsers.

Thankfully, JavaScript has us covered here with a special function that will automatically tell us if any value is an array or not: `Array.isArray()`. Simply give it a value and it will tell you if it’s an array or not an array by returning true or false.

```javascript
console.log(Array.isArray([])); // true
console.log(Array.isArray({})); // false

let fakeArray = Object.create(Array.prototype);
console.log(fakeArray instanceof Array); // true
console.log(Array.isArray(fakeArray)); // false

```

More info:

- [Array.isArray on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [March 13, 2020, 6:55pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-is-it-an-array/643155/2 "2020-03-13T18:55:55Z")

</div>

How does Array.isArray know when an array is an array?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 13, 2020, 9:45pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-is-it-an-array/643155/3 "2020-03-13T21:45:45Z")

</div>

The answer is both simple and complicated. Simply, Array.isArray knows something is an array because it knows what an array is. As obvious as that sounds, it’s also basically how it works under the hood.

More specifically, isArray looks at the value its given and determines if that value is an array _exotic object_. A future tip was planned to cover exotic objects, but maybe that will get moved up and we can look at those in more detail a bit sooner [Edit: added - [Exotic Objects](http://forum.kirupa.com/t/javascript-tip-of-the-day/641803/38)]. Basically, some object types have special characteristics that make them special - something giving them a little more magic than what’s possible with normal objects. Arrays, for example, have a magical `length` property which automatically updates when you add or remove an indexed property. If you assign a value to `[0]` in an empty array object, its `length` magically becomes `1`. This magic is not something you’d find in ordinary objects which makes arrays exotic. Runtimes are able to keep track and recognize exotic objects and isArray uses that to know if a certain value is, in fact, an array.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [March 13, 2020, 10:08pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-is-it-an-array/643155/4 "2020-03-13T22:08:18Z")

</div>

Thanks! I look forward to your tip on exotic objects.

---

<div class="post-metadata">

**Author:** ![tbo](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@tbo](https://forum.kirupa.com/u/tbo)\
**Post date:** [March 13, 2020, 10:43pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-is-it-an-array/643155/5 "2020-03-13T22:43:44Z")

</div>

After reading this i decided to check my projects for game.isShit and sure enough it returns true, odd to find this since i wrote the games myself, but i cant argue with the compiler

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [March 13, 2020, 10:56pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-is-it-an-array/643155/6 "2020-03-13T22:56:03Z")

</div>

Have you tried setting it to false? You might be on to game of the year.

---

<div class="post-metadata">

**Author:** ![tbo](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@tbo](https://forum.kirupa.com/u/tbo)\
**Post date:** [March 13, 2020, 11:03pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-is-it-an-array/643155/7 "2020-03-13T23:03:32Z")

</div>

It wont let me I’ve been trying to figure it out for 10 years
