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 The Solution

Question about booleans in conditional statements

When testing booleans in a conditional statement, do you not have to include whether you want to test for it being true vs false? Below is some code I wrote for one of the JavaScript Workshops. However, when I checked it against the official solution, they had the conditional written as: (isNan(num1) || isNaN(num2))

I'm confused as to why you do not need to declare whether you're testing it to be true or false.

if (num2 == 0) {
    alert("The second number is 0. You can't divide by zero. Reload and try again.");
} else if { (isNaN(num1) === true || isNaN(num2) === true) {
    alert("At least one of your inputs is not a number. Please try again!");
} else {
    message = "<h1>Math with the numbers " + num1 + " and " + num2 + "</h1>";
    document.write(message);
}

3 Answers

Jean-Paul REMAN
Jean-Paul REMAN
6,004 Points

An exemple : var hello = "Hi";

you could write : if (hello) { .... ; } // a variable hello exists then hello is true. if ( hello === true ) is the same of if ( hello ) if ( sun ) {..... ;} else {........ ; } // There is no variable named sun , then sun is false. if ( sun === true ) is the same of if ( sun )

Jon Wood
Jon Wood
9,884 Points

The isNan function evaluates to return a true or false. So if the isNan(num1) evaluates to true, the if statement will interpret it like if(true).

Peter Retvari
seal-mask
.a{fill-rule:evenodd;}techdegree
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 Points

Dears,

Could you please help me? I also solved the solution and wanted to ask the same question, however I found this topic. But I still don't understand why "(isNan(num1) || isNaN(num2))" is equivalent with (isNan(num1)=true || isNaN(num2)=true). I wonder if you could explain it. Thanks