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 Variables Null and Undefined

Undefined.

Why would you need to know if a variable is known or not? Is it for debugging code? What does typeof do?

1 Answer

Richard Duncan
Richard Duncan
5,568 Points

typeof returns a string value indicating the type of the object whose type is to be returned. In plain terminology it will evaluate the type of variable and return a string containing the data type stored in that variable.

It's very useful when you're writing functions that have arguments where you would like to set a default value. If the function is called with no parameters set for the arguments then you can check the typeof with an if statement, check to see if the arguments are say undefined and if they are assign default values to the variable.

There is more information on typeof here

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof