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

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