JavaScript if else Statements
Conditional statements in JavaScript allow you to execute different code blocks based on specific conditions. There are three main conditional statements in JavaScript:
1 1. if Statement 2 2. if else Statement 3 3. if else if Statement
If Statement
- The
if
statement is used to execute a block of code if a specific condition is met. The syntax for anif
statement is as follows:
1if (condition) { 2 // Code to be executed if the condition is true 3}
For example:
1let x = 10; 2 3if (x > 5) { 4 console.log("x is greater than 5"); // output 5}
In the example above, the if
statement checks if x
is greater than 5. If the condition is true, the code inside the curly braces will be executed, and "x is greater than 5" will be logged to the console.
If Else Statement
- The
if...else
statement is used to execute a block of code if a specific condition is met, and a different block of code if the condition is not met. The syntax for anif...else
statement is as follows:
1if (condition) { 2 // Code to be executed if the condition is true 3} else { 4 // Code to be executed if the condition is false 5}
For example:
1let x = 10; 2 3if (x > 15) { 4 console.log("x is greater than 15"); 5} else { 6 console.log("x is not greater than 15"); // output 7}
In the example above, the if...else
statement checks if x
is greater than 15. If the condition is true, the first block of code will be executed, and "x is greater than 15" will be logged to the console. If the condition is false, the second block of code will be executed, and "x is not greater than 15" will be logged to the console.
If Else If Statement
- The
if...else if
statement is used to execute a block of code from multiple conditions. The syntax for anif...else if
statement is as follows:
1if (condition1) { 2 // Code to be executed if condition1 is true 3} else if (condition2) { 4 // Code to be executed if condition1 is false and condition2 is true 5}
For example:
1let x = 8; 2 3if (x > 15) { 4 console.log("x is greater than 15"); 5} else if (x > 5) { 6 console.log("x is greater than 5 but not greater than 15"); // output 7}
In the example above, the if...else if
statement checks if x
is less than 15. If the first condition is false, the first block of code will not executed, the program will move on to the second condition and check if x
is greater than 5. If this condition is true, the second block of code will be executed, and "x is greater than 5 but not greater than 15" will be logged to the console.
If Else If Else Statement
- The
if...else if...else
statement is used to execute a block of code from multiple conditions. We can add multiple else if statements to the condition block if needed. The syntax for anif...else if...else
statement is as follows:
1if (condition1) { 2 // Code to be executed if condition1 is true 3} else if (condition2) { 4 // Code to be executed if condition1 is false and condition2 is true 5} else { 6 // Code to be executed if both condition1 and condition2 are false 7}
For example:
1let x = 4; 2 3if (x > 15) { 4 console.log("x is greater than 15"); 5} else if (x > 5) { 6 console.log("x is greater than 5 but not greater than 15"); 7} else { 8 console.log("x is not greater than 5"); // output 9}
In the example above, the if...else if...else
statement checks if x
is greater than 15. If the first condition is true, the first block of code will be executed, and "x is greater than 15" will be logged to the console. If the first condition is false, the program will move on to the second condition and check if x
is greater than 5. If this condition is true, the second block of code will be executed, and "x is greater than 5 but not greater than 15" will be logged to the console. If both conditions are false, the program will execute the code in the else
block, and "x is not greater than 5" will be logged to the console.
Nested if statement
- The
if
statement is used to execute a block of code if a specific condition is met. The syntax for anif
statement is as follows:
1if (condition_1) { 2 // Code to be executed if the first condition is true 3 if (condition_2) { 4 // Code to be executed if the sencond condition is true 5 } 6}
For example:
1let x = 20; 2 3if (x > 5) { 4 console.log("x is greater than 5"); // output 5 if (x > 10) { 6 console.log("x is greater than 10"); // output 7 } 8}
In the example above, the if
statement checks if x
is greater than 5. If the condition is true, the code inside the curly braces will be executed, and "x is greater than 5" and it will check the second condition statement is x
is greater than 10 if true it will execute code inside the block.