LESSON 19 OF 50

Choose between two branches

Use if…else for alternative outcomes.

Official sourceECMAScript — if statementReviewed: 2026-08-26
STEP 1 · LEARN

How it works

if executes the first branch when the condition is true. else provides the branch used when that condition is false.

Correct example
const hour = 21;
if (hour < 18) {
  console.log('Deschis');
} else {
  console.log('Închis');
}

21 is not less than 18, so the else branch runs.

Common mistake
if (hour < 18) { console.log('Deschis'); }

Without else, the false case produces no message.

STEP 2 · APPLY

Your exercise

Print “Deschis” before hour 18, otherwise “Închis”.

STEP 3 · RUN

Write and run the code

script.jsJAVASCRIPT
Isolated consoleisolated
Preparing the runtime…
Run the code to see the result.
The draft stays only in this browser.Isolated QuickJS · no DOM, session or network
STEP 4 · CHECK

What needs to pass

0%not checked
  • hour is declared with const.
  • The if…else structure checks hour < 18 and produces output in both branches.
  • The output is Închis.
Explain the solution logic

One condition chooses between two mutually exclusive outcomes.