thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

09.01 · Ternary & conditional challenges / Learn · 3 min

Choose a value with a ternary

Two possible messages, one condition. Choose a value before printing it.

Activity 1 of 6 in Ternary & conditional challenges

All courses

JavaScript Basics / 09.01

Choose a value with a ternary

Not started

Read condition, true value and false value as one expression that produces a result.

More options

Your lesson progress

Reading & walkthrough: not startedGuided practice: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Rob compares two message cards, considers a question-mark prop, and selects one response.
Rob’s comic strip · Ternary & conditional challenges

Three parts, one chosen value

Read condition ? valueIfTrue : valueIfFalse from left to right. JavaScript checks the condition, then evaluates only the chosen expression.

A ternary produces a value, so you can store the result in a variable. Keep the printing step separate while learning to trace it.

Choose a label
example.jsjavascript · example
const isReady = true;
const label = isReady ? "Ready" : "Keep working";
console.log(label);

Keep the choice easy to read

Use a ternary for a short choice between two values. Keep if/else when branches need several steps, or when a ternary becomes hard to read.

Your source also demonstrates printing directly in each branch. Choosing the value first keeps the expression easier to reuse. You do not need nested ternaries for the challenges that follow.

The same choice written with if/else
example.jsjavascript · example
const isReady = false;
let label;
if (isReady) {
  label = "Ready";
} else {
  label = "Keep working";
}
console.log(label);

Choose a value with a ternary expression

Work through 4 focused steps. Read the explanation, type each requested change and check it before moving on. Your own code carries forward.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

4 steps · Saved work · Instant feedback

YOUR TURN

Try the examples on this page

Choose Load this code into the editor beside an example, then edit and Run code here. Predict what will print before running it. This is an exploration workspace, so there is no pass/fail challenge. Its saved work stays separate from the next practice activity.

Load an example, edit it and run it. Word wrap is enabled.

Work in VS Code instead

Copy the starter into a file named practice.js in VS Code. If Node.js is installed, open the terminal in that folder and run node practice.js. Compare the output with the task above. Automated challenge checks are available in the browser editor.

Code example
example.jsjavascript · example
const isReady = true;
const label = isReady ? "Ready" : "Keep working";
console.log(label);

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

A ternary expression chooses one value: condition ? valueIfTrue : valueIfFalse.