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!

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


Library return values challenge followup question

This is in reference to the Javascript Foundations Library Return Values challenge that reads as such:

create a function named 'arrayCounter' that takes in a parameter which is an array. The function must return the length of an array passed in or 0 if a 'string', 'number' or 'undefined' value is passed in.

I found great help over here, but I have a followup question, which is basically Why does this work?

In the code, we are simply saying that something is an array, undefined, a number, etc. Does this only work because we are using typeOf? And how come the function itself only needs this to know what an array is?

function arrayCounter (array)

So yeah, my basic question is how come this all works? Is it because of simply how Javascript works? Are we fudging for the sake of a code challenge?

I'm just wondering mostly because this code challenge seemed to pull on knowledge we didn't really learn—we merely sorta kinda could pick up on. I want to get a solid foundation not just of what the answer is, but why the answer is. :-) Thanks in advance!