![]() |
Java Beginners Course: Exploring Control Flow and Conditional Statements in JavaScript |
Now that you understand the basics of JavaScript syntax and variables, it's time to explore control flow and conditional statements. These features allow you to control the flow of execution in your programs and make decisions based on certain conditions.
Conditional Statements
Conditional statements allow you to execute different blocks of code based on whether a specified condition evaluates to true or false. In JavaScript, the most common conditional statements are:
1. if Statement: The `if` statement evaluates a specified condition. If the condition is true, the code block inside the `if` statement is executed. Example:
```javascript
let age = 20;
if (age >= 18) {
console.log("You are an adult");
}
```
2. else Statement: The `else` statement follows an `if` statement and specifies a block of code to be executed if the condition in the `if` statement evaluates to false. Example:
```javascript
let hour = 15;
if (hour < 12) {
console.log("Good morning");
} else {
console.log("Good afternoon");
}
```
3. else if Statement: The `else if` statement allows you to specify additional conditions to test if the previous conditions in the `if` statement are false. Example:
```javascript
let time = 20;
if (time < 12) {
console.log("Good morning");
} else if (time < 18) {
console.log("Good afternoon");
} else {
console.log("Good evening");
}
```
Comparison Operators
Conditional statements rely on comparison operators to compare values and determine whether a condition is true or false. Some commonly used comparison operators include:
- `==`: Equal to
- `!=`: Not equal to
- `>`: Greater than
- `<`: Less than
- `>=`: Greater than or equal to
- `<=`: Less than or equal to
Logical Operators
Logical operators are used to combine multiple conditions in conditional statements. The three main logical operators in JavaScript are:
- `&&` (Logical AND): Returns true if both conditions are true.
- `||` (Logical OR): Returns true if at least one condition is true.
- `!` (Logical NOT): Returns the opposite boolean value of the condition.
Switch Statement
The `switch` statement provides an alternative way to handle multiple conditions in JavaScript. It evaluates an expression and executes the code block associated with the first matching `case` label. Example:
```javascript
let day = "Monday";
switch (day) {
case "Monday":
console.log("Today is Monday");
break;
case "Tuesday":
console.log("Today is Tuesday");
break;
default:
console.log("Unknown day");
}
```
Conclusion
In this chapter, we've covered control flow and conditional statements in JavaScript. These features allow you to make decisions and execute different blocks of code based on specified conditions. Understanding conditional statements, comparison operators, logical operators, and the `switch` statement is essential for writing flexible and interactive JavaScript programs. In the next chapter, we'll explore loops, which enable you to repeat actions multiple times in your code.