[Solved] Difference between a==a?a:b and a?a:b


The first statement will return always true except the case when a is NaN

Why NaN == NaN returns false ?

Because the JS spec says so:

  • If Type(x) is Number, then
    • If x is NaN, return false.
    • If y is NaN, return false.

The second statement will return true only if a is not a falsy variable.

When I say falsy i mean undefined, null , 0, . A falsy value is a value that translates to false when evaluated in a Boolean context.

Truthy:

In JavaScript, a truthy value is a value that is considered true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, “”, null, undefined, and NaN).

Falsy:

A falsy value is a value that translates to false when evaluated in a Boolean context.

1

solved Difference between a==a?a:b and a?a:b