JavaScript If Else
The if-else
statement is used to execute a block of code if
a certain condition is true.
If
the condition is not true, an optional else statement can be used to execute a different block of code.
Additionally, multiple conditions can be tested using the else if
statement.
if-else
syntax:
if (condition) {
// code to execute if condition is true
} else if (condition2) {
// code to execute if condition2 is true
} else {
// code to execute if neither condition nor condition2 is true
}
Conditional Statements
Conditional statements are used to execute different code blocks based on whether a certain condition is true or false.
Conditional statements are used to execute different blocks of code based on whether a certain condition is true or false.
The if statement is the most basic conditional statement and is used to execute a block of code if a certain condition is true.
The else statement can be used with the if statement to execute a different block of code if the condition is false.
The else if statement can be used to test multiple conditions after the initial if statement.
The switch statement provides an alternative way of handling multiple conditions and allows you to execute different blocks of code based on the value of a variable or expression.
All of these conditional statements are based on Boolean expressions, which evaluate to either true or false.
It's important to use proper indentation and syntax when writing conditional statements to make your code more readable and avoid errors.
The if
Statement
The if
statement is a fundamental part of JavaScript that allows you to execute a block of code conditionally.
The if
statement evaluates a Boolean expression and executes the code inside the block if
the expression is true
.
if
syntax:
if (boolean_expression) {
// code to execute if the expression is true
}
In the syntax above:
boolean_expression
is a Boolean expression that evaluates to eithertrue
orfalse
.- If the expression is
true
, the code inside the curly braces will be executed. If the expression isfalse
, the code inside the curly braces will be skipped.
Example:
let temperature = 25;
if (temperature > 30) {
console.log("It's hot outside!");
}
In this example:
- The
if
statement checks whether the value of thetemperature
variable is greater than 30. If
it is, the code inside the curly braces is executed, which logs a message to the console.If
the condition is not true, theconsole.log()
statement is skipped.
The else
Statement
The else
statement is a complement to the if
statement in JavaScript, allowing you to execute a block of code when the condition specified in the if
statement is false
.
if-else
syntax:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
In the syntax above:
- The code inside the curly braces after the
if
statement is executed if the condition is true. - If the condition is false, the code inside the curly braces after the else statement is executed.
Example:
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
In this example:
- If the age is greater than or equal to 18, the message "You are an adult" will be logged to the console.
- If the age is less than 18, the message "You are a minor" will be logged instead.
Note that the else
statement must be used in conjunction with an if
statement. You cannot use an else
statement on its own. If you only want to execute code when a condition is false without any other condition, you can use a single if
statement without an else
statement.
The else if
Statement
The else if
statement is a useful extension of the if
and else
statements in JavaScript, allowing you to test multiple conditions in a series.
if-else if
syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if both condition1 and condition2 are false
}
In the syntax above:
- The
if
statement checks the first condition, and if it is true, the code inside the first curly braces is executed. - If it is false, the
else if
statement checks the next condition, and if it is true, the code inside the second curly braces is executed. - If none of the conditions are true, the code inside the
else
block is executed.
Example:
let number = 10;
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.");
}
In this example:
- The first
if
statement checks if thenumber
variable is greater than 0. If it is, the message "The number is positive" will be logged to the console. - If not, the
else if
statement checks if the number is less than 0. - If it is, the message "The number is negative" will be logged to the console.
- If neither condition is true, the
else
block is executed, and the message "The number is zero" is logged.