Skip to content
Advertisement

react render Logical && vs Ternary operator

In the react render() when the value of x equals 1 both the logical && and ternary operator will display Hello and both are syntactically correct. I always used the && when I don’t want to show the else part of my condition, but I have come across one code base where most of the places they used ternary operator with null instead of &&. Is there any performance gain or any other advantage of using one method over the other?

JavaScript

Advertisement

Answer

There’s no significant performance difference, but because 0 and empty string "" are “falsy” in JavaScript I always opt for the ternary operator so the next person to edit my code knows my exact intention.

Example:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement