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 Numbers The Math Object Random Number Challenge – Solution

Not understanding the condition inside the if clause

The condition inside the if clause is supposed to evaluate whether or not the highNumber variable is a number. So, why wouldn't it be something like this...

if ( highNumber == number) { }

?

I don't understand how if ( highNumber ) { }

alone is a condition.

3 Answers

Cameron Childres
Cameron Childres
11,818 Points

Hi Esther,

The magic here comes from two things: parseInt() and truthy/falsy values.

First, parseInt() takes the string given to it from the prompt and converts it to a number value. If it cannot convert it to a number value it returns NaN ('not a number'):

const highNumber = parseInt(inputHigh)

A value being "truthy" means that when it is looked at in a boolean context it is considered to be true. Conveniently for us numbers are "truthy" and NaN is "falsy". So when you have:

if (highNumber) {}

It will either be looking at a number value or NaN -- nothing else will be produced by parseInt(). If it's a number it's true, if it's NaN it's false.

Hope this helps! Let me know if you have any questions.

Thank you, this is the first time I've heard of truthy/falsy values. I'm going to read up on them a bit more

Thank you. I feel like this was a key concept that wasn't talked about enough in the video