Grokking React Fundamentals
0% completed
Conditional Rendering with Ternaries
With &&, the login example can show a welcome message or nothing. It cannot show "Please log in" to a visitor who is logged out, because && has no second branch. For an either-or choice, JavaScript has the conditional operator, usually called the ternary operator because it takes three operands:
condition ? valueIfTrue : valueIfFalse
JavaScript evaluates condition first. If it is truthy, the whole expression evaluates to valueIfTrue, and if it is falsy, the expression evaluates to valueIfFalse, so only the chosen branch is ever evaluated
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%