Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Foundations Functions Return Values

Ilya Liverts
Ilya Liverts
9,677 Points

How can I check if variable is array?

My code does not pass: ''' <!DOCTYPE html> <html lang="en"> <head> <title>JavaScript Foundations: Functions</title> <style> html { background: #FAFAFA; font-family: sans-serif; } </style> </head> <body> <h1>JavaScript Foundations</h1> <h2>Functions: Return Values</h2> <script>

  function arrayCounter(a)
  {

    if(typeof a == 'string')
    {
    return 0;
    }
  if(typeof a == 'undefined')
    {
     return 0;
    }
    if(typeof a == 'number')
    {
     return 0;
    }
     if(typeof a == 'Array')
    {
    return a.lenght;
    }

  }

</script>

</body> </html> '''

2 Answers

Chris Malcolm
Chris Malcolm
2,909 Points

You misspelled length ,perhaps that has to do with error

Sean T. Unwin
Sean T. Unwin
28,672 Points
 if(typeof a == 'Array')
    {
    return a.lenght; /* There is a typo in length */
    }

An Array is a subset of an Object so typeof will not work.

Substitute typeof for instanceof will correct the issue. There is a slight syntax change, however. See the following.

 if(a instanceof Array)
 {
    return a.length;
 }

edit: As mentioned below by Dino Paškvan, Array.isArray() is available for modern browsers and in certain use-cases would be more robust.

 /**
  * You still need the error checks included
  *      i.e. not string, number, or undefined
  *      and to also return 0 if so for the code challenge to pass
  **/
  if (Array.isArray(a)) { return a.length; }

It's important to note that using the instanceof operator is not a completely safe method of checking for arrays (in multi-frame DOM environments).

That's why Array.isArray() was introduced in ES5 and it can be easily polyfilled with Object.prototype.toString.call() for earlier environments.

Sean T. Unwin
Sean T. Unwin
28,672 Points

Indeed, Dino Paškvan.

I was going to include Array.isArray() but opted against because of some older browser compatibility. I will note, though, that this way is the best way, in my opinion, as long as the target audience's browser of the webpage is compatible. I did not want to include Object.prototype.toString.call() because I did not want to make a potentially already confusing topic to be lost on the reader.

For the purposes of the question, and more importantly, the task at hand - the code challenge - I opted for what I thought would help the most without conflating the topic.

I thank you for your response though, because a potential JavaScript developer should be aware of the intricacies of Arrays as well as Objects.

I will update my answer to include Array.isArray() as going forward there should be less use for the polyfill as up-to-date browsers will make use of the ES5 spec.