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

Anthony Giuliano
Anthony Giuliano
9,569 Points

Functions / Return Values Code Challenge

Having trouble with this one. I remember from the video that you can test if an argument is empty with a conditional like:

if(var === 'undefined')

But I don't recall going over how to test if the variable contains a string or a number.

3 Answers

You would use the typeof operator to test whether a variable holds the value of a string or a number

For example:

var one = 1; var str = "String";

if (typeof one === 'number') { console.log("This is a number"); }

if (typeof str === "string") { console.log("This is a string"); }

Anthony Giuliano
Anthony Giuliano
9,569 Points

Thanks for the answer! I had literally just started the video over again and caught his use of the typeof operator. Although interestingly I found that typeof array returns 'Object', which I wasn't expecting.

Thanks again.

Laura Nielsen
Laura Nielsen
7,200 Points

Thanks this was very helpful =)