An if statement helps your program make decisions by running code only when certain conditions are true.
If statement in Javascript
Introduction
Syntax
Javascript
if (condition) { // code to run if condition is true }
The condition is a question your program asks, like 'Is it raining?'.
If the condition is true, the code inside the curly braces runs. If false, it skips.
Examples
age is 18 or more, then prints a message.Javascript
if (age >= 18) { console.log('You can vote'); }
Javascript
if (temperature < 0) { console.log('It is freezing'); }
isRaining is true.Javascript
if (isRaining) { console.log('Take an umbrella'); }
Sample Program
This program checks if the score is 60 or more. If yes, it prints a congratulation message.
Javascript
const score = 75; if (score >= 60) { console.log('You passed the test!'); }
Important Notes
Always use parentheses () around the condition.
Curly braces {} group the code that runs when the condition is true.
If the condition is false, the code inside the if block is skipped.
Summary
An if statement runs code only when a condition is true.
Use it to make your program decide what to do next.
Remember to put the condition in parentheses and the code to run inside curly braces.
Practice
1. What does an
if statement do in JavaScript?easy
Solution
Step 1: Understand the purpose of an if statement
An if statement checks a condition and runs code only when that condition is true.Step 2: Compare options with the definition
Only Runs code only if a condition is true matches this behavior exactly.Final Answer:
Runs code only if a condition is true -> Option DQuick Check:
If statement = runs code if true [OK]
Hint: If it runs code only when true, it's an if statement [OK]
Common Mistakes:
- Confusing if with loops
- Thinking if declares variables
- Mixing if with function definitions
2. Which of the following is the correct syntax for an
if statement in JavaScript?easy
Solution
Step 1: Recall JavaScript if syntax
The condition must be inside parentheses, and the code block can be inside curly braces or a single statement without braces.Step 2: Check each option
if (condition) console.log('Yes'); correctly uses parentheses around the condition and a statement without braces.Final Answer:
if (condition) console.log('Yes'); -> Option BQuick Check:
Parentheses around condition = correct syntax [OK]
Hint: Condition must be in parentheses for if [OK]
Common Mistakes:
- Omitting parentheses around condition
- Using colons instead of braces
- Adding 'then' keyword (not in JS)
3. What will be the output of this code?
let x = 10;
if (x > 5) {
console.log('Big');
} else {
console.log('Small');
}medium
Solution
Step 1: Evaluate the condition x > 5
Since x is 10, which is greater than 5, the condition is true.Step 2: Determine which block runs
The if block runs and prints 'Big'. The else block is skipped.Final Answer:
Big -> Option CQuick Check:
10 > 5 is true, so output is 'Big' [OK]
Hint: Check if condition is true to pick output [OK]
Common Mistakes:
- Choosing else output when condition is true
- Confusing variable value with output
- Ignoring else block
4. Find the error in this code:
if x == 5 {
console.log('Equal');
}medium
Solution
Step 1: Check if statement syntax
In JavaScript, the condition must be inside parentheses after if.Step 2: Identify the error
The code misses parentheses aroundx == 5, causing a syntax error.Final Answer:
Missing parentheses around condition -> Option AQuick Check:
Condition needs parentheses in if [OK]
Hint: Always put condition inside parentheses in if [OK]
Common Mistakes:
- Omitting parentheses around condition
- Confusing == and === (both valid but different)
- Thinking semicolon is mandatory after console.log
5. You want to print 'Adult' if age is 18 or more, otherwise 'Minor'. Which code correctly uses an if statement?
hard
Solution
Step 1: Check condition for age 18 or more
The condition should be age >= 18 to include 18 and above.Step 2: Verify syntax and logic
if (age >= 18) { console.log('Adult'); } else { console.log('Minor'); } uses correct syntax and condition. if age >= 18 then { console.log('Adult'); } else { console.log('Minor'); } uses invalid 'then'. if (age > 18) { console.log('Adult'); } else { console.log('Minor'); } excludes age 18. if (age = 18) { console.log('Adult'); } else { console.log('Minor'); } uses assignment (=) instead of comparison (== or >=).Final Answer:
if (age >= 18) { console.log('Adult'); } else { console.log('Minor'); } -> Option AQuick Check:
Use >= and correct syntax for if-else [OK]
Hint: Use >= for 18 or more, no 'then' keyword [OK]
Common Mistakes:
- Using assignment (=) instead of comparison
- Using 'then' keyword (not in JS)
- Using > instead of >= to include 18
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
