15 min +50 XP
Lesson 21Step 1 of 5

If Statement

Make decisions in your code

What are If Statements?

If statements let your code make decisions! They run certain code only when a condition is true. Think of it like: "IF it's raining, THEN bring an umbrella."

Basic Syntax

javascript
1
2
3
4
5
6
7
8
9
10
11
if (condition) {
  // Code runs only if condition is true
}
 // Example:
const age = 16
 if (age >= 16) {
  console.log('You can drive!')
}
// Prints: You can drive!

Key Parts:

  • if - The keyword that starts the statement
  • (condition) - What to check (must be true or false)
  • {curly braces} - Contains the code to run if true