Lauro Silva

Watch the video

NEW

Prefer your lessons in video format? Watch for free on egghead:

It mighty be tempting to use the operator typeof , on the array.

1const food = typeof ["🧀", "🍤", "🍔", "🥞"]
2
3console.log(food)

But as you can see, it returns an object. This is because arrays are technically objects. Instead, we can use isArray() a static method provided by the Array built-in object.

1const list = typeof ["🧀", "🍤", "🍔", "🥞"]
2const list2 = ["🧀", "🍤", "🍔", "🥞"]
3const list3 = true
4
5console.log(list)
6console.log(Array.isArray(list2))
7console.log(Array.isArray(list3)) // returns false

To summarize, if the value is an Array, true is returned; otherwise, false is.

© Lauro Silva, LLC. All rights reserved.