HOLLA TECH - LEARN

The if Statement
 

Very often when you write code, you want to perform different actions based on different conditions.
You can do this by using conditional statements in your code.

Use if to specify a block of code that will be executed if a specified condition is true. 

if (condition) {
   statements

 

The statements will be executed only if the specified condition is true

Example: 

var myNum1 = 7;
var myNum2 = 10;
if (myNum1 < myNum2) {
   alert(“JavaScript is easy to learn.”);

 

Result: 

Holla Tech - Learn JavaScript

NOTE!
As seen in the picture above, the JavaScript alert() method is used to generate a popup alert box that contains the information provided in parentheses.

The if Statement
 

This is another example of a false conditional statement.

var myNum1 = 7;
var myNum2 = 10;
if (myNum1 > myNum2) {
   alert(“JavaScript is easy to learn.”);

 

As the condition evaluates to false, the alert statement is skipped and the program continues with the line after the if statement’s closing curly brace.

NOTE!
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.

BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE JAVASCRIPT MAIN COURSE PAGE. 

JAVASCRIPT MAIN COURSE PAGE

 


© License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT