In this lesson, you will learn how to check if a JavaScript value is an array with isArray()
a static method provided by the Array
built-in object. This is an alternative to using the operator typeof
, since arrays are technically objects.
Instructor: [0:00] When passing a value, sometimes you want to be absolutely sure that whatever you're passing is actually an array. It might be tempting choose a operator typeof, but as you can see here, it will evaluate to an object. This is technically true since arrays are objects.
[0:21] An alternative is to use the built-in method provided by array, called isArray, a static method. Let's pass in our list number two.
[0:34] I'll create list number two here. Let's console.log this. You can see here it returns true.
[0:44] It will return false if it's not an array. Let's pass in a string here. Let's call it list number three. We can see here it returns false.
[0:59] To recap, we use that built-in static method called isArray provided by array to determine if our value that we're passing in is an array. True, if it's an array. False, if it's not.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!