Welcome, everyone! Today, we're diving into conditional statements in JavaScript. These statements allow our programs to make decisions based on specific conditions we define.
Think of a conditional statement like making decisions in real life. You decide what to wear based on the weather, right? Similarly, JavaScript uses conditional statements to change how our code behaves depending on different situations.
In JavaScript, conditions are checks that evaluate to either true or false. We use comparison operators to compare values and create these conditions. Here are some common ones:
===
: Checks if two values are exactly the same!==
: Checks if two values are not exactly the same>
: Checks if the value on the left is greater than the value on the right<
: Checks if the value on the left is less than the value on the right>=
: Checks if the value on the left is greater than or equal to the value on the right<=
: Checks if the value on the left is less than or equal to the value on the rightThese operators help us create conditions that tell our code what to do based on different inputs and situations.
The if
statement is basic but powerful in JavaScript. It allows us to execute a block of code only if a specified condition is true. If the condition is false, the code block is skipped.
Here's how an if/else statement works:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Let's consider a real-world example: determining if a user is old enough to vote.
let age = 18;
if (age >= 18) {
console.log('You are eligible to vote.');
} else {
console.log('You are not eligible to vote yet.');
}
// Output: You are eligible to vote.
In this example, if the variable age
is 18 or older, the message "You are eligible to vote." is displayed. If not, the message "You are not eligible to vote yet." is displayed.
Sometimes, you need to check multiple conditions. The else if
statement allows you to specify a new condition if the previous condition is false, creating a chain of conditions.
Here's how an if/else if/else statement looks:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition1 is false and condition2 is true
} else {
// Code to execute if both condition1 and condition2 are false
}
Let's apply this to another real-world example: classifying a number as positive, negative, or zero.
let number = 5;
if (number > 0) {
console.log('The number is positive.');
} else if (number < 0) {
console.log('The number is negative.');
} else {
console.log('The number is zero.');
}
// Output: The number is positive.
In this case, depending on the value of number
, one of the messages "The number is positive.", "The number is negative.", or "The number is zero." will be displayed.
The switch
statement provides another way to handle multiple conditions in JavaScript. It's useful when you have a variable or expression that can have different values, and you want to execute different blocks of code based on those values.
Here's how a switch statement works:
switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
default:
// Code to execute if none of the cases match
}
The switch
statement evaluates the expression
and executes the code block associated with the first matching case
. If no matches are found, it executes the code in the default
block.
Let's illustrate this with an example: displaying a message based on the day of the week.
let day = 'Monday';
switch (day) {
case 'Monday':
console.log('Start of the work week.');
break;
case 'Wednesday':
console.log('Midweek day.');
break;
case 'Friday':
console.log('End of the work week.');
break;
default:
console.log('Regular day.');
}
// Output: Start of the work week.
In this example, the switch statement evaluates the variable day
and prints a message based on its value. Since day
is set to 'Monday'
, the message "Start of the work week." is printed.
The switch
statement is particularly useful when you have multiple conditions to check against a single value. It's more concise and readable than chaining multiple if
and else if
statements.
The switch
statement is particularly useful when you have multiple conditions to check against a single value. It's more concise and readable than chaining multiple if
and else if
statements.
Let's break down the switch statement in more detail:
expression
and compares it to the values in the case
statements.expression
matches the value1
, the code block below it is executed.expression
matches a case
, the code below it is executed until a break
statement is encountered. This prevents the code from falling through to the next case.expression
doesn't match any of the case
values, the code block below the default
statement is executed.Remember to always include a break
statement after the code block of each case
to prevent the code from falling through to the next case.
Let's explore some more examples using the switch statement:
These examples showcase the versatility and usefulness of the switch statement in real-world scenarios.
Let's explore some practical uses of conditional statements in real-world scenarios:
Conditional statements (if/else
, else if
, switch
) are essential tools in JavaScript that help us create dynamic and responsive applications. They allow us to control the flow of our programs based on various conditions.
Practice using these statements in your own projects to become more familiar with their usage. Experiment with different conditions and see how they affect the behavior of your code. The more you practice, the more comfortable you'll become with JavaScript's conditional statements.