Conditional Statements in JavaScript
The if
statement executes a block of code only if a specified condition is true.
var d = new Date();
var time = d.getHours();
if (time < 10) {
document.write("<b>Good morning</b>");
}
Example 2
// Display "Lunch-time!" if the time is 11
var time = new Date().getHours();
if (time == 11) {
document.write("<b>Lunch-time!</b>");
}
Note: When comparing variables, always use == (double equals) instead of a single equals sign (=).
2. if...else Statement
If a condition is true, one block of code will run. If it is false, another block of code will execute.
Syntax
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Example
var time = new Date().getHours();
if (time < 10) {
document.write("Good morning!");
} else {
document.write("Good day!");
}
3. if...else if...else Statement
Use this when you want to check multiple conditions.
Syntax
if (condition1) {
// Executes if condition1 is true
} else if (condition2) {
// Executes if condition2 is true
} else {
// Executes if none of the conditions are true
}
Example
var time = new Date().getHours();
if (time < 10) {
document.write("<b>Good morning</b>");
} else if (time > 10 && time < 16) {
document.write("<b>Good day</b>");
} else {
document.write("<b>Hello World!</b>");
}
The switch Statement
Use the switch statement to select one of many code blocks to execute.
Syntax
switch (expression) {
case value1:
// Code block 1
break;
case value2:
// Code block 2
break;
default:
// Code to execute if no match is found
}
Example
var day = new Date().getDay();
switch (day) {
case 5:
document.write("Finally Friday");
break;
case 6:
document.write("Super Saturday");
break;
case 0:
document.write("Sleepy Sunday");
break;
default:
document.write("I'm looking forward to this weekend!");
}
Loops in JavaScript
1. for Loop
Use a for loop when you know how many times the loop should run.
Syntax
for (var i = startValue; i <= endValue; i++) {
// Code to execute in each iteration
}
Example
for (var i = 0; i <= 10; i++) {
document.write("The number is " + i + "<br>");
}
Output:
The number is 0
The number is 1
The number is 2
...
The number is 10
2. while Loop
Use a while loop when you don't know how many times it should run beforehand.
Syntax
while (condition) {
// Code to be executed
}
Example
var i = 0;
while (i <= 10) {
document.write("The number is " + i + "<br>");
i++;
}
3. do...while Loop
This loop always runs at least once, even if the condition is false.
Syntax
do {
// Code to be executed
} while (condition);
Example
var i = 0;
do {
document.write("The number is " + i + "<br>");
i++;
} while (i < 0);
Output:
The number is 0
Even though the condition i < 0 is false, the loop executes once before checking the condition.
Break & Continue Statements
1. break Statement
Exits a loop immediately when a specific condition is met.
Example
for (var i = 0; i <= 10; i++) {
if (i == 3) {
break;
}
document.write("The number is " + i + "<br>");
}
Output:
The number is 0
The number is 1
The number is 2
The loop stops at 3 because of break.
2. continue Statement
Skips the rest of the loop iteration and jumps to the next iteration.
Example
for (var i = 0; i <= 10; i++) {
if (i == 3) {
continue;
}
document.write("The number is " + i + "<br>");
}
Output:
The number is 0
The number is 1
The number is 2
The number is 4
...
The number is 10
The number 3 is skipped because of continue.
Conclusion
This guide covered conditional statements (if, else, switch), loops (for, while, do...while), and loop control (break, continue) in JavaScript. These concepts are fundamental for controlling program flow efficiently. 🚀
To mark this module as complete, you must finish this quiz. Once submitted, you'll need to wait 2 hours before attempting it again.