<Below this line, add a link to the EXACT exercise that you are stuck at.>

Does the if/else statement in JavaScript refer to true and false?

So in the code below, the ‘Howwlll!’ will only be printed when the variables moonPhase and foggyNight are full and true, right? And if the variable foggyNight is false, this will cause the program to print “Invalid moon phase”. Am I correct?


var moonPhase = ‘full’;
var foggyNight = false;

if (moonPhase === ‘full’ && foggyNight) {
} else if (moonPhase === ‘mostly full’) {
console.log(‘Arms and legs are getting hairier’);
} else if (moonPhase === ‘mostly new’) {
console.log(‘Back on two feet’);
} else {
console.log(‘Invalid moon phase’);

<do not remove the three backticks above>

Yes. Because you’re using && which check the two conditions and will only return true if both are true. Thus allowing the console.log('Howwlll!") to run.

In this case, yes. There is a But.

A more clearer way to say this is that everything that does not fall into 3 conditions you have stated above, will fall into category of else.

Notice you don’t specify the condition of foggyNight in the:

else if (moonPhase === 'mostly full')
else if (moonPhase === 'mostly new')

So, even if your foggyNight = false but moonPhase = 'mostly full' or moonPhase = 'mostly new', it will print arms and legs are getting hairier or back on two feet respectively instead of invalid moon phase.

To summarize, what will print'Invalid moon phase', it is not only when foggyNight = false, but it can be anything that don’t match the 3 conditions in the if and else if code, even if when foggyNight = true.


var moonPhase = 'a quarter full';
var foggyNight = true

it will still print'Invalid moon phase' even foggyNight = true, because moonPhase = 'a quarter full' does not fall into any if conditions you stated.

Hope this clarifies. @seasail, I like the name. :slight_smile:


Wow. I get it now. Thank you!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.