Javascript && operator versus nested if statements: what is faster?
The performance difference is negligible. The &&
operator won't check the right hand expression when the left hand expression evaluates false
. However, the &
operator will check both regardless, maybe your confusion is caused by this fact.
In this particular example, I'd just choose the one using &&
, since that's better readable.
If you're concerned about performance, then make sure that a==b
is more likely to fail than c==d
. That way the if
statement will fail early.
Like nested if
s, &&
is lazy.
The expression a && b
will only evaluate b
if a
is truthful.
Therefore, the two cases should be completely identical, in both functionality and performance.