Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Test if argument is an array

My attempt to test an argument to see if it is an array:

if (typeof arr === 'array') // where arr was a function parameter

failed miserably!

In a video lesson there was an example: if (typeof name === 'undefined') { ...}

I gather the code challenge is expecting a similar approach but can one test for an array somehow using the typeof operator?

Thanks;

Ted

2 Answers

This should help.

var value = ["really", "cool", "stuff"];

if (value instanceof Array) {
    console.log('value is Array!');
    } else {
    console.log('Not an array');
}

Thanks for the reply Riley. I was doing one of the code challenges in which they did an example using typeof. Sometimes it seems that its expected that you use the approach they demonstrated. But I think I prefer your suggestion. Thanks again; Ted