What if your code could think and decide for you, just like a helpful friend?
Why If–else statement in Javascript? - Purpose & Use Cases
Imagine you are sorting mail by hand. You have to decide if each letter goes to the 'urgent' pile or the 'normal' pile based on the address. Doing this for hundreds of letters takes a lot of time and you might mix some up.
Manually checking each letter is slow and tiring. You might forget rules or make mistakes, like putting urgent mail in the wrong pile. This wastes time and causes confusion.
The if-else statement lets your program make decisions automatically. It checks conditions and chooses the right action quickly and without mistakes, just like a smart helper sorting mail perfectly every time.
if (score > 50) { console.log('Pass'); } else { console.log('Fail'); }
console.log(score > 50 ? 'Pass' : 'Fail');
It enables your program to react differently to different situations, making it smart and flexible like a helpful friend.
Think about a traffic light system that changes colors based on time or cars waiting. If-else statements help decide when to switch lights to keep traffic flowing smoothly.
If-else helps your code make choices.
It saves time and reduces mistakes compared to manual checking.
It makes programs smart and adaptable.
Practice
if-else statement do in JavaScript?Solution
Step 1: Understand the purpose of if-else
An if-else statement lets the program decide between two paths depending on a condition.Step 2: Match the description to the options
Only It chooses between two actions based on a condition. describes choosing between two actions based on a condition.Final Answer:
It chooses between two actions based on a condition. -> Option DQuick Check:
If-else = choose between two actions [OK]
- Confusing if-else with loops
- Thinking if-else defines functions
- Believing if-else creates variables
Solution
Step 1: Recall JavaScript if-else syntax
JavaScript requires parentheses around the condition and braces for blocks: if (condition) { } else { }Step 2: Compare options to correct syntax
Only if (condition) { } else { } uses parentheses and braces correctly without extra keywords.Final Answer:
if (condition) { } else { } -> Option AQuick Check:
Correct syntax uses parentheses and braces [OK]
- Omitting parentheses around condition
- Adding 'then' keyword (not in JS)
- Missing braces for code blocks
let age = 18;
if (age >= 18) {
console.log('Adult');
} else {
console.log('Minor');
}Solution
Step 1: Evaluate the condition age >= 18
Since age is 18, the condition age >= 18 is true.Step 2: Determine which block runs
The if block runs and prints 'Adult'. The else block is skipped.Final Answer:
Adult -> Option AQuick Check:
Condition true -> if block runs -> 'Adult' [OK]
- Thinking 18 is less than 18
- Confusing else block runs when condition is true
- Expecting syntax error
let score = 75;
if score >= 60 {
console.log('Pass');
} else {
console.log('Fail');
}Solution
Step 1: Check if-else syntax
JavaScript requires parentheses around the condition in if statements.Step 2: Identify the missing parentheses
The code has if score >= 60 { ... } but it should be if (score >= 60) { ... }Final Answer:
Missing parentheses around the condition -> Option CQuick Check:
if condition must be in ( ) [OK]
- Forgetting parentheses around condition
- Thinking semicolon is required after console.log block
- Confusing else usage
let num = 7; // Choose the correct code snippet
Solution
Step 1: Understand how to check even numbers
A number is even if remainder when divided by 2 is zero: num % 2 === 0.Step 2: Check each option's condition
if (num % 2 === 0) { console.log('Even'); } else { console.log('Odd'); } correctly checks num % 2 === 0. Others either use wrong operator or logic.Final Answer:
if (num % 2 === 0) { console.log('Even'); } else { console.log('Odd'); } -> Option BQuick Check:
Even check uses num % 2 === 0 [OK]
- Using division (/) instead of modulo (%)
- Checking if remainder is truthy instead of zero
- Reversing even and odd conditions
Ready to prove it?
Pass the test and this pattern moves unknown
