The if else Statement part 2

JavaScript Tutorial
Conditionals and Loops
The if else Statement
24
2/2
         

The else Statement



The example below demonstrates the use of an if...else statement.
var myNum1 = 7;
var myNum2 = 10;
if (myNum1 > myNum2) {
alert("This is my first condition");
}
else {
alert("This is my second condition");
}
Try It Yourself

The above example says:
If myNum1 is greater than myNum2, alert "This is my first condition";
Else, alert "This is my second condition".

The browser will print out the second condition, as 7 is not greater than 10.
There is also another way to do this check using the ? operator: a > b ? alert(a) : alert(b).
Fill in the blanks to have a valid if...else statement:
var age = 25;
 
 
 (age >= 18) {
   alert("Allowed.");
}
 
 
 {
   alert("Not allowed.");
}

Comments

Popular posts from this blog

Comparison Operators part 2

Break and Continue part 1

Module 2