JavaScript if Statement
The JavaScript if statement evaluates a specified condition and executes a block of code if the condition is true. It provides a fundamental way to implement decision-making when coding, allowing for more dynamic and responsive programs. This statement checks the condition and, based on its Boolean evaluation, decides the execution path: code within the if block runs only when the condition evaluates to true.
Examples
Basic Usage
let number = 10;
if (number > 5) {
console.log('The number is greater than 5.');
}
This will output: The number is greater than 5.
Multiple Conditions
let age = 20;
let hasPermission = true;
if (age >= 18 && hasPermission) {
console.log('Access granted.');
}
This will output: Access granted.
Using Comparison Operators
let score = 75;
if (score >= 50) {
console.log('You passed the exam.');
}
This outputs: You passed the exam.
JavaScript else Statement
The JavaScript else statement executes a block of code when the condition in the if statement is false. If the condition evaluates to true, the code inside the if block runs; otherwise, the code inside the else block executes.
Examples
Consider a scenario where you need to check if a user is 18 years old or older. You can use the if/else structure to differentiate between two actions based on the user's age:
let age = 20;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are not an adult.");
}
In this example, the console will output You are an adult. because the condition age >= 18 is true. If age were less than 18, it would output You are not an adult..
Another example involves determining if a number is even or odd:
let number = 7;
if (number % 2 == 0) {
console.log("The number is even.");
} else {
console.log("The number is odd.");
}
Here, the output will be The number is odd. since 7 divided by 2 has a remainder of 1, making the condition number % 2 == 0 false.
JavaScript else if Statement
The JavaScript else if statement provides a way to decide among multiple blocks of code to be executed. It extends the functionality of the if statement by allowing multiple, sequential checks. If the initial if condition evaluates to false, the else if conditions are tested in the order they appear. If an else if condition evaluates to true, the corresponding block of code runs.
Examples
Consider the following example, which checks the value of the variable grade and logs different messages based on its value:
let grade = 87;
if (grade >= 90) {
console.log("Excellent");
} else if (grade >= 80) {
console.log("Good");
} else if (grade >= 70) {
console.log("Average");
} else if (grade >= 60) {
console.log("Fair");
} else {
console.log("Poor");
}
In this code: - If grade is 90 or above, it logs "Excellent". - If grade is between 80 and 89, it logs "Good". - If grade is between 70 and 79, it logs "Average". - If grade is between 60 and 69, it logs "Fair". - For any grade below 60, it logs "Poor".
Here's another example that determines how to greet a user based on the current time:
let hour = new Date().getHours();
if (hour < 12) {
console.log("Good morning");
} else if (hour < 17) {
console.log("Good afternoon");
} else {
console.log("Good evening");
}
- This script checks the current hour. - If it's less than 12, it logs "Good morning". - If it's between 12 and 16, inclusive, it logs "Good afternoon". - Otherwise, it logs "Good evening".
Nested if...else Statement
A nested if...else statement in JavaScript allows for multiple levels of condition checking, providing a way to execute different code blocks based on various conditions within each other. This structure is useful for handling complex decision-making processes where multiple, sequential questions need to be addressed.
Examples
Consider the scenario where you want to classify a person's age group and check if they are eligible for a youth discount or a senior discount at a cinema:
let age = 23;
if (age < 18) {
console.log("Underage");
} else {
if (age < 30) {
console.log("Youth discount applies");
} else {
if (age >= 65) {
console.log("Senior discount applies");
} else {
console.log("No discount applies");
}
}
}
Output: Youth discount applies
For a more compact form, else-if statements can also be used:
let age = 68;
if (age < 18) {
console.log("Underage");
} else if (age < 30) {
console.log("Youth discount applies");
} else if (age >= 65) {
console.log("Senior discount applies");
} else {
console.log("No discount applies");
}
Output: Senior discount applies
Conclusion
The JavaScript if/else statement fundamentally directs the flow of execution depending on condition evaluations, making it indispensable for decision-making in code. It evaluates a condition and executes a block of code if the condition is true; if not, it can execute an alternative block under the else clause. This binary decision structure allows developers to write more concise and readable code, handling multiple conditions efficiently through else if extensions. Proper use of if/else statements reduces complexity and increases the reliability of JavaScript applications by enabling conditional operations and responses based on user inputs or internal logic checks. These statements are the backbone of dynamic interaction within programming, ensuring that applications behave correctly under various states and inputs.