Conditional Statements
Conditional statements are used in JavaScript to make decisions based on different conditions. They allow you to execute different blocks of code based on the evaluation of a condition. The most common conditional statements in JavaScript are if
statements, if...else
statements, and switch
statements.
if statement
The if
statement is used to execute a block of code if a specified condition evaluates to true
. Here's the syntax of an if
statement:
1if (condition) {
2 // code to be executed if the condition is true
3}
For example, let's say we have a variable temperature
that holds the current temperature. We can use an if
statement to check the value of temperature
and log a message based on the condition:
1const temperature = 25;
2
3if (temperature < 0) {
4 console.log('It's freezing outside!');
5}
In this example, if the temperature
is less than 0, the message 'It's freezing outside!'
will be logged to the console.
if...else statement
The if...else
statement is used to execute a block of code if a specified condition evaluates to true
, and another block of code if the condition evaluates to false
. Here's the syntax of an if...else
statement:
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, let's modify the previous example to include an else
block to handle the case when the temperature is not below 0:
1const temperature = 25;
2
3if (temperature < 0) {
4 console.log('It's freezing outside!');
5} else {
6 console.log('It's not freezing outside.');
7}
In this example, if the temperature
is less than 0, the message 'It's freezing outside!'
will be logged to the console. Otherwise, the message 'It's not freezing outside.'
will be logged.
switch statement
The switch
statement is used to perform different actions based on different conditions. It provides a more concise syntax when multiple conditions need to be evaluated. Here's the syntax of a switch
statement:
1switch (expression) {
2 case value1:
3 // code to be executed if the expression matches value1
4 break;
5 case value2:
6 // code to be executed if the expression matches value2
7 break;
8 ... // additional cases
9 default:
10 // code to be executed if none of the cases match the expression
11}
For example, let's say we have a variable day
that holds the name of the day. We can use a switch
statement to execute different code based on the value of day
:
1const day = 'Monday';
2
3switch (day) {
4 case 'Monday':
5 console.log('It's the start of the week.');
6 break;
7 case 'Friday':
8 console.log('It's the end of the week.');
9 break;
10 default:
11 console.log('It's a regular day.');
12}
In this example, if the value of day
is 'Monday'
, the message 'It's the start of the week.'
will be logged to the console. If the value of day
is 'Friday'
, the message 'It's the end of the week.'
will be logged. For any other value of day
, the message 'It's a regular day.'
will be logged.
xxxxxxxxxx
// Example of a conditional statement
const temperature = 25;
if (temperature < 0) {
console.log('It's freezing outside!');
} else if (temperature < 10) {
console.log('It's cold outside.');
} else if (temperature < 20) {
console.log('It's cool outside.');
} else {
console.log('It's warm outside!');
}