Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära else and else if | Understanding Program Flow and Decisions
Conditional Statements and Loops in JavaScript

bookelse and else if

When you need your code to make decisions based on more than just a simple yes or no, you use branching logic. This means your program can follow different paths depending on what's happening. A great way to picture this is to imagine a traffic light. When you approach a traffic light, you have to make a decision.

  • If the light is red, you stop;
  • If it's yellow, you get ready to stop;
  • If it's green, you go.

In JavaScript, you can handle these different possibilities using if, else if, and else statements. This lets your code respond to many situations, not just two.

1234567891011
let light = "yellow"; if (light === "red") { console.log("Stop!"); } else if (light === "yellow") { console.log("Get ready to stop."); } else if (light === "green") { console.log("Go!"); } else { console.log("Unknown signal."); }
copy
1234567891011
let light = "green"; if (light === "red") { console.log("Stop!"); } else if (light === "yellow") { console.log("Get ready to stop."); } else if (light === "green") { console.log("Go!"); } else { console.log("Unknown signal."); }
copy
question-icon

Match each traffic light color to the correct message printed by the code.

- red:- yellow:- green:

Click or drag`n`drop items and fill in the blanks

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 4

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 7.69

bookelse and else if

Svep för att visa menyn

When you need your code to make decisions based on more than just a simple yes or no, you use branching logic. This means your program can follow different paths depending on what's happening. A great way to picture this is to imagine a traffic light. When you approach a traffic light, you have to make a decision.

  • If the light is red, you stop;
  • If it's yellow, you get ready to stop;
  • If it's green, you go.

In JavaScript, you can handle these different possibilities using if, else if, and else statements. This lets your code respond to many situations, not just two.

1234567891011
let light = "yellow"; if (light === "red") { console.log("Stop!"); } else if (light === "yellow") { console.log("Get ready to stop."); } else if (light === "green") { console.log("Go!"); } else { console.log("Unknown signal."); }
copy
1234567891011
let light = "green"; if (light === "red") { console.log("Stop!"); } else if (light === "yellow") { console.log("Get ready to stop."); } else if (light === "green") { console.log("Go!"); } else { console.log("Unknown signal."); }
copy
question-icon

Match each traffic light color to the correct message printed by the code.

- red:- yellow:- green:

Click or drag`n`drop items and fill in the blanks

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 4
some-alt