Conditional Operator in JavaScript – What Is a Ternary Operator?
A conditional operator is an alternate (shortcut) way of writing an if...else
statement.
Syntax of a Ternary Operator in JavaScript
Example of a Ternary Operator in JavaScript
The snippet above instructs the computer to log "Today is special!"
on the browser’s console if the time is less than 21:00
. Else, the system should log out "Last minutes embody great opportunities!"
.
Example of a Ternary Operator in a JavaScript Function
The snippet above instructs the computer to log "Qualified"
on the browser’s console if age
is greater than 35
. Else, the system should log out "Not Qualified!"
.
Keep in mind that the if...else
equivalence of the ternary operator above is like so: