Developed By
Gautam Kumar - Full stack developer
DEEP DIVE INTO
In JavaScript, if and else are conditional statements used for making decisions on your code.
These statements permit you to execute one of a kind blocks of code based totally on whether a distinct condition is real or false. Here's a comprehensive rationalization of if and else statements:
The if
statement is used to execute a block of code if
a specified condition is true. It has the following syntax:
javascriptif (condition) {
// Code to be executed if the condition is true
}
condition: An expression that evaluates to either true or false. If the condition is true, the code inside the block is executed; otherwise, it's skipped.
javascriptconst age = 18;
if (age >= 18) {
console.log('You are an adult.');
}
In this example, the code inside the if block is executed because the condition age >= 18 is true.
The else
statement is used in conjunction with an if statement to execute a block of code if the if condition is false. It has the following syntax:
javascriptif (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
If the condition in the if
statement is true, the code inside the if block is executed, and the else block is skipped.
If the condition in the if
statement is false, the code inside the if block is skipped, and the code inside the else block is executed.
javascriptconst age = 15;
if (age >= 18) {
console.log('You are an adult.');
} else {
console.log('You are not an adult.');
}
In this example, because the condition age >= 18 is false, the code inside the else block is executed.
You can chain multiple conditions together using else if statements. An else if statement is used when you want to check additional conditions after the initial if condition.
javascriptif (condition1) {
// Code to be executed if condition1 is true
} else if (condition2) {
// Code to be executed if condition1 is false and condition2 is true
} else {
// Code to be executed if both condition1 and condition2 are false
}
JavaScript checks the conditions in order from top to bottom, and the first true condition's block is executed.
javascriptconst score = 75;
if (score >= 90) {
console.log('A');
} else if (score >= 80) {
console.log('B');
} else if (score >= 70) {
console.log('C');
} else {
console.log('D');
}
In this example, the code outputs 'C' because the first condition that evaluates to true is score >= 70.
You can nest if statements within other if statements to create more complex decision structures. This allows you to check multiple conditions in a hierarchical manner.
javascriptconst isAdult = true;
const hasLicense = true;
if (isAdult) {
if (hasLicense) {
console.log('You can drive.');
} else {
console.log('You cannot drive without a license.');
}
} else {
console.log('You are not an adult.');
}
In this example, the code first checks if the person is an adult and then, if they have a license.
Conditional statements like if
, else
, and else if
are essential for controlling the waft of your JavaScript code. They permit your packages to make selections and execute one-of-a-kind code paths primarily based on precise situations, that's a key a part of growing dynamic and responsive applications.