Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.
The ternary operator is a short way to express conditionals, and it works in this way:
<condition> ? <expression> : <expression>
The <condition> is evaluated as a boolean, and upon the result, the operator runs the first expression if the condition is true.
Or it runs the second expression if the condition is false.
In other words:
isTrue ? /* do this */ : /* do that */
Example usage:
const running = true
(running === true) ? console.log('stop') : console.log('run')
In this example we check if running equals true, and if so we log 'stop'. Otherwise we log 'run'.
Lessons in this unit:
| 0: | Introduction |
| 1: | Comparison operators |
| 2: | `if` statements |
| 3: | How to use `else` |
| 4: | `switch` |
| 5: | ▶︎ The ternary operator |