How to use the ternary operator in JavaScript

The Ternary operator is basically an if/else block in condensed form. The general format is:

condition ? expr1 : expr2

Which means: If the condition is true, use expr1 , otherwise use expr2. You can assign the result of this to a variable:

    const message = isLoggedIn ? "Hello user!" : "Please log in"

Check out this CodeSandbox:

This is part of the JavaScript bite-size series of posts. Check out the rest here!