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

Richard Zili
Richard Zili
6,451 Points

In an array, how does one pass a value of "string" or "number" without actually specifying a string or number?

This is in regards to the code challenge in the "Return Values" section of the JavaScript lessons

3 Answers

To get the value type of a variable, you would use the typeof operator before the variable.

Within your function, you want to find the value type of the parameter that is passed into it, so you would use typeof parameter_variable and then check to see if it is equal to 'string', 'number', or 'undefined'.

See below:

function arrayCounter(array){
      if (typeof array === 'string' || typeof array === 'number' || typeof array === 'undefined'){
          return 0;
      }
      return array.length;
  }
Richard Zili
Richard Zili
6,451 Points

Thanks for your help!

Thanks also by me!

thanks :D