Spot the Assignment in if
Find the Mistake Easy
Question
This if block always runs. What is wrong?
Code
const expected = 10;
const actual = 5;
if (actual = expected) {
console.log("Match!");
}
Hint
= assigns, === compares.
Explanation
The condition uses = (assignment) instead of === (comparison). actual = expected assigns 10 to actual and evaluates to 10, which is truthy, so the if block always runs. The fix is if (actual === expected) — with actual = 5 and expected = 10, this would be false and 'Match!' would not print.
Related Resources
Related Lessons
- Conditions
Learn about = vs ===.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.