JavaScript Conditional Statements
- ★ Conditional statements are used to perform different actions based on different conditions.
- ★ Use if to specify a block of code to be executed, if a specified condition is true.
- ★ Use else to specify a block of code to be executed, if the same condition is false.
- ★ Use else if to specify a new condition to test, if the first condition is false.
- ★ Use switch to specify many alternative blocks of code to be executed
if statement
Syntax:if(condition){ code to be executed if the condition is true }
Example
Note: Some special charaters can not be displayed if those charaters are used directly in your HTML file.& becomes & < becomes < > becomes > " becomes " ' becomes '
let a = 1; let b = 2; if(a < b){ document.getElementById("p1").innerHTML = 'a < b'; }
if...else statment
Syntax:if(condition){ code to be executed if the condition is true } else{ code to be executed if the condition is false }
Example
let a = 1; let b = 2; if(a < b){ document.getElementById("p1").innerHTML = 'a < b'; } else{ document.getElementById("p1").innerHTML = 'a >= b'; }
if...else if...else statement
Syntax:if(condition1){ code to be executed if the condition 1 is true } else if(condition 2){ code to be executed if the condition 2 is true } else{ code to be executed if the condition 2 is false }
Example
let a = 1; let b = 2; if(a < b){ document.getElementById("p1").innerHTML = 'a < b'; } else if(a > b){ document.getElementById("p1").innerHTML = 'a > b'; } else{ document.getElementById("p1").innerHTML = 'a==b'; }
Switch statement
- ★ The switch statement is used to perform different actions based on different conditions.
switch(expression){ case x: code to be executed if expression == x break; case y: code to be executed if expression == y break; case z: code to be executed if expression == z break; default: code to be executed }
- ★ The switch expression is evaluated once;
- ★ The value of the expression is compared with the values of each case;
- ★ If there is a match, the associated block of code will be executed.
- ★ If there is no match, the default code block is executed.
<p id="demo"></p> <script> let day; switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; break; } document.getElementById("demo").innerHTML = "Today is " + day; </script>
Can case Labels in switch Statements Be Any Data Type?
In JavaScript, the case labels in a switch statement must be values that can be strictly compared (===) to the expression inside switch().Allowed Data Types for case Labels:
- 1. Using Numbers in case
let num = 2; switch (num) { case 1: console.log("One"); break; case 2: console.log("Two"); break; default: console.log("Unknown number"); }
let fruit = "apple"; switch (fruit) { case "banana": console.log("It's a banana."); break; case "apple": console.log("It's an apple."); break; default: console.log("Unknown fruit."); }
let isLoggedIn = true; switch (isLoggedIn) { case true: console.log("User is logged in."); break; case false: console.log("User is logged out."); break; }
let x = 10; switch (x % 2 === 0) { // This evaluates to true or false case true: console.log("Even number"); break; case false: console.log("Odd number"); break; }