Hi there, I'm Marcos!

❓ The conditional operator in JavaScript

The conditional operator is a JavaScript expression that evaluates and returns one of two expressions depending on a specified condition:

<condition> ? <expression if true> : <expression if false>

It’s very useful if you want to assign a value to a variable based on a condition or as an if...else alternative:

// assignment expression
const visibility = isMobile ? 'hidden' : 'visible';

// if...else alternative
isUserRegistered ? logInUser() : signUpUser();

Note that only the winning expression is the one evaluated, so in cases where you have functions as expressions, only the winning function will be evaluated (in our example above, the user would be signed up or logged in, never both of them)