JS-043

Beginner4 min~4 min read#if#condition#control-flow
if
Run code when a condition is true
Goal
Learn to run code only when a condition returns true.
Explanation
- if is the basic decision-making statement in JavaScript.
- First, the condition inside the parentheses is checked.
- If the result is true, the code runs.
- If the result is false, the code is skipped.
- if is the foundation of almost all program logic.
Analogy
If it is sunny outside, you go for a walk. Otherwise, you do nothing. That is exactly how if works.
Code
if (isSunny) {
console.log("Go outside");
}Result: Go outside
Common mistake
Beginners often think the code inside if always runs. It actually runs only when the condition is true.
Remember
- ✦ if checks a condition.
- ✦ The code runs only when the condition is true.
- ✦ When the condition is false, the block is skipped.
Quick Quiz
When does the code inside an if statement run?