LESSON 20 OF 50

Choose with the conditional operator

Use condition ? value1 : value2 for a short choice.

Official sourceECMAScript — conditional operatorReviewed: 2026-08-26
STEP 1 · LEARN

How it works

The conditional operator evaluates the condition before ?. When it is truthy, it returns the first expression; otherwise it returns the expression after :.

Correct example
const tasks = 3;
const status = tasks > 0 ? 'Activ' : 'Gol';
console.log(status);

tasks > 0 is true, so status receives “Activ”.

Common mistake
const status = 'Activ';

The hard-coded value does not demonstrate a choice between two outcomes.

STEP 2 · APPLY

Your exercise

Declare status with a conditional operator based on tasks > 0 and print status.

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
  • tasks and status are declared with const.
  • status uses tasks > 0 ? … : …
  • The output is Activ.
Explain the solution logic

The conditional expression produces the value assigned to status, and the console confirms the selected branch.