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 JavaScript Foundations Functions Return Values

Andy Durette
Andy Durette
31,183 Points

JavaScript Functions Question

I am wondering why my code here

  function arrayCounter (x) {
        if (typeof x !== 'array'){
         return 0;
      }else
        return x.length;
      }

Will not pass this JavaScript challenge http://teamtreehouse.com/library/return-values

However this JavaScript will

  function arrayCounter (array) {
        if (typeof array === 'undefined' ||typeof array === 'string'||typeof array === 'number') {
            return 0; 
        }
        return array.length;
      }

Doesn't the two code blocks do the same thing since I set that if the typeof was not equal to array it should return 0.

1 Answer

Alexander Costa
Alexander Costa
11,464 Points

typeof thinks of arrays as objects

other wise your code says "if type of x is not a an array return 0" which would not work because like the second code suggests we are looking for typeof undefined, string, and number.