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