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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Octavian Runceanu](https://uploads.teamtreehouse.com/production/profile-photos/11654469/micro_IMG_20190921_181353.jpg)
Octavian Runceanu
6,143 Pointsif (highNumber) question
I don't understand the following block: if (highNumber) { } What does this condition do? It's the same as if (highNumber == true) ?
And if yes, how is compared a number with a boolean . I do not get it. Can someone please explain this concept to me?
3 Answers
![Jason Larson](https://uploads.teamtreehouse.com/production/profile-photos/11651180/micro_JLarson-Crop3.jpg)
Jason Larson
8,359 PointsIt works in JavaScript the same as if (highNumber == true)
if highNumber were a boolean, but it's not a boolean, so how does it work? I understand your confusion. The answer is that when not provided with a value to compare to, the conditional checks if it is "truthy" or "falsey" by deciding that some values are inherently true and some are inherently false. For example, when checking numbers, a zero ( 0 ) is considered false, whereas a 1 (or any other number) is considered true. This way of coding makes it easier and shorter to write code that accomplishes the same thing, without having to put in lots of checks for bad values. In reality, if (highNumber) {}
is really more like: if (highNumber > 0 && highNumber != null && highNumber != NaN && highNumber != undefinded && highNumber != false){}
. This is very useful when you might be checking against a value that might be undefined or evaluates to an error, like NaN (which is one thing that could happen in this challenge). I hope that helps.
Another thing I should mention is that I don't really like the alert message, as it tells the user to enter a number, but if you put in a zero, it will also fail the condition, so the message should really say "You need to provide a number greater than one"
![notarobot](https://uploads.teamtreehouse.com/production/profile-photos/11870453/micro_notarobotprofilepicture.webp)
notarobot
Full Stack JavaScript Techdegree Student 2,607 PointsThanks Jason. Great explanation.
I got stuck thinking about this too.
I initially tried if( highestNumber === NaN) but it didn't work.
I wanted to share a solution that tests NaN using the if( isNaN(variable) ). I had to dig a little to find it. I hope this helps someone like myself.
const highestNumber = +prompt('Want A random number. Provide the highest number in the range');
if( isNaN(highestNumber) || highestNumber === 0 ) {
document.querySelector('main').innerHTML = `<h2>You need to provide a number that is greater than 0. Refresh and try again.</h2>`;
} else {
const finalRandomNumber = Math.floor ( Math.random () * highestNumber ) + 1
const finalMessage = ( `A random number between 1 and ${highestNumber} is <b>${finalRandomNumber}</b>.`);
document.querySelector('main').innerHTML = `<h2>${finalMessage}</h2>`
}
![Alvaro Davila](https://secure.gravatar.com/avatar/f90ee86365b358fbccc406117c256ca2?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Alvaro Davila
949 PointsWhen using parseInt(highNumber), if the conversion from a string to a number is successful, it will return a valid number, which is a truthy value. Thus, the condition will evaluate to true (there's no need to compare directly to true with === true).
Conversely, if the conversion fails and parseInt cannot convert the string to a number, it will return NaN (Not-a-Number), which is a falsy value. Consequently, the condition will evaluate to false.
Using === true is redundant because the if statement already performs the implicit conversion to a boolean. For example, writing if (parseInt(highNumber) === true) is unnecessary; if (parseInt(highNumber)) is sufficient and more idiomatic in JavaScript.
Tony Shangkuan
7,200 PointsTony Shangkuan
7,200 PointsThanks for the clarification!! I had the same question!