Skip to content
dreamcode
dreamcode
Map
Ternary operator
Lesson 10 of 48
+15 XP on finish
JAVASCRIPT CLIMBSChapter 2 · JS Conditionals & Logic

Ternary Operator

The ternary operator is a shorthand for simple if-else blocks. It takes a condition followed by a question mark ?, then the expression to run if true, a colon :, and the expression to run if false.

Worked example

How it reads

  • score >= 50 is evaluated as the condition
  • Pass is returned if the condition is true, Fail if false
Cloud tip: Ternaries are expressions, meaning they resolve to a value that can be assigned directly to a variable.
index.js
JAVASCRIPT
real JavaScript, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Add a second ternary that sets icon to "sun" or "moon" from sunIsUp, set sunIsUp to false, and log both: night then moon.

Press Run to check your work.