Skip to main content
D
JavaScript
JavaScript If...Else

JavaScript If...Else

JavaScript if statements run code only when conditions are true.

Read Time
5 min read
Difficulty
Beginner
Last Updated
Jun 15, 2026
Version
v1.0.0

Introduction

JavaScript uses conditional statements to make decisions. Developers tell the computer to check a condition, like if a user is logged in. If true, the code shows the dashboard; if false, it shows the login screen.

Example

Example
1/* this checks a condition */
2if (score > 50) {
3  result = "You pass!";
4} else {
5  result = "You fail.";
6}

Key Points

  • The `if` block runs when the condition is true.
  • The `else` block runs when the condition is false.
  • The `else if` block tests a new condition.
  • Conditions go inside parentheses.

Up Next

Continue your journey with the next topic.

Go to JS Switch