JavaScript > JavaScript Fundamentals > Control Structures > if...else statements
Basic if...else Statement
This snippet demonstrates the fundamental usage of the if...else statement in JavaScript, enabling conditional code execution based on a boolean expression.
Core Concept: Conditional Execution
The if...else statement allows you to execute different blocks of code depending on whether a specified condition is true or false. The if block executes if the condition evaluates to true. The else block (optional) executes if the condition evaluates to false.
Code Example
This code checks if the age variable is greater than or equal to 18. If it is, the message "You are eligible to vote." is printed to the console. Otherwise, the message "You are not eligible to vote yet." is printed.
let age = 20;
if (age >= 18) {
console.log("You are eligible to vote.");
} else {
console.log("You are not eligible to vote yet.");
}
Explanation of the Code
1. We declare a variable named age and assign it the value 20.
2. The if statement checks if age >= 18. This expression evaluates to true because 20 is greater than or equal to 18.
3. Since the condition is true, the code inside the if block is executed, printing "You are eligible to vote." to the console.
4. If age were less than 18, the code inside the else block would be executed instead.
Real-Life Use Case
if...else statements are commonly used for validating user input, determining access rights, displaying different content based on user roles, handling error conditions, and implementing game logic.
Best Practices
if statements simple and easy to understand.{} even for single-line blocks to avoid confusion.switch statement when dealing with multiple conditions based on the same variable.if...else statements as they can become difficult to maintain.
Interview Tip
Be prepared to explain the purpose of if...else statements, provide examples of their use, and discuss best practices for writing clear and maintainable conditional code. Understand the difference between if...else and switch statements.
When to use them
Use if...else statements when you need to execute different code blocks based on a condition that can be evaluated as either true or false. They are especially useful for handling binary decisions or branching logic.
Memory footprint
The memory footprint of if...else statements is minimal. They only consume a small amount of memory to store the condition and the pointers to the code blocks to be executed. The impact on overall performance is negligible unless used excessively in computationally intensive operations.
Alternatives
Alternatives to if...else statements include:
Pros
Cons
switch statement instead).
FAQ
-
What happens if the 'else' block is omitted?
If theelseblock is omitted, and the condition in theifstatement is false, then the code simply proceeds to the next statement after theifblock without executing any additional code. -
Can I nest 'if...else' statements?
Yes, you can nestif...elsestatements. However, deeply nested statements can become difficult to read and maintain. Consider alternative approaches likeswitchstatements or breaking down the logic into smaller functions if the nesting becomes too complex.