Learn JavaScript
Lesson 1 - Introduction To JavaScript
Lesson 2 - Basic JavaScript Syntax
Lesson 4 - Functions
Lesson 5 - Basic Data Structures
Lesson 6 - Basic String Operations
Lesson 7 - Basic Array Operations
Lesson 8 - Exception Handling
Lesson 9 - Packages
Lesson 10 - User Input
Conditional statements allow your program to make decisions, done by combining if
, else if
, and else
statements.
Where:
if
statement executes a code block when the condition is true
.else if
statement is an additional condition that executes a code block when the if
statement is false
, but the additional statement is true
.else
statement works like a default action, executing a code block when both the if
and else if
statements are false
.Take a look at the following example:
let x = 10; if (x > 10) { console.log("x is greater than 10"); } else if (x < 10) { console.log("x is less than 10"); } else { console.log("x is equal to 10"); }
Here, we compare the value of x
with 10. The code first checks if x > 10
, then checks if x < 10
. If both of the conditions are false
, it executes the default action using the else
statement.
Now try to change the value of x
.