Javascript Boolean Operator Question 1

Introduction

What is the output of the following Javascript code:

let userAge = 15; 

if (userAge = 0) {; 
    console.log("So you're a baby !"); 
} else if ( userAge < 0  | userAge > 200) 
    console.log("I think you may be lying about your age"); 
else { 
   console.log("That's a good age"); 
} 


Compile time error

There are two mistakes on the line:

if (userAge = 0) {; 

First, it has only one equals sign instead of two in the if's condition/

It means userAge will be assigned the value of 0 rather than userAge being compared to 0.

The second fault is the semicolon at the end of the line-statements.

if and loops such as for and while don't require semicolons.

So the line should be:

if (userAge == 0) { 

The next fault is with these lines:

else if ( userAge < 0  | userAge > 200) 
   console.log("I think you may be lying about your age"); 
else { 

The correct operator for a boolean OR is ||.




PreviousNext

Related