1

I was checking the equality of all the falsey values from the list here:

Below code for three falsey values namely null, undefined and NaN doesn't print hi:

if(null == false) console.log('hi');
if(undefined == false) console.log('hi');
if(NaN == false) console.log('hi');

All other falsey values end up printing the text hi as shown below:

if('' == false) console.log('hi');
if(0x0 == false) console.log('hi');
if(false == false) console.log('hi');
if(0.0 == false) console.log('hi');
if(0 == false) console.log('hi');

Can anyone help me understand the reason behind this behavior?

Update for future readers:

Three interesting reads if you are trying to wrap your head around weirdness of falsy values and equality operators in JavaScript:

  1. why null==undefined is true in javascript
  2. Why does (true > null) always return true in JavaScript?
  3. What exactly is Type Coercion in Javascript?
RBT
  • 24,161
  • 21
  • 159
  • 240
  • 5
    The semantics of the `==` operator are not the same as the rules for evaluation of arbitrary values as boolean. – Pointy Sep 20 '17 at 00:45
  • Read here about type coercion you will understand it bit better: https://stackoverflow.com/questions/19915688/what-exactly-is-type-coercion-in-javascript – pegla Sep 20 '17 at 00:48
  • 3
    Specifically, the `==` (or "loose equality") operator [compares two values for equality _after_ converting both values to a common type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Loose_equality_using) – Hamms Sep 20 '17 at 00:49
  • Never use loose comparison to compare against a boolean. Try `if('0' == false) console.log('hi');`. Surprised? If you want to know what's happening exactly, [have a look at the spec](https://ecma-international.org/ecma-262/8.0/#sec-abstract-equality-comparison). That algorithm is easy to follow. – Felix Kling Sep 20 '17 at 01:45
  • @FelixKling `'0'` is simply a non-empty string so it will pass the equality test with `false`. Any non-empty string is a truthy value. – RBT Sep 20 '17 at 01:49
  • Did you run the code? – Felix Kling Sep 20 '17 at 01:50
  • Yes. It prints `hi`. – RBT Sep 20 '17 at 01:50
  • I just reread your comment. If non-empty strings are truthy, why should they be equal to `false`? `'foo'` is also non-empty and *not* equal to `false`. Your question is about falsy values. Aren't you surprised that both `''` and `'0'` are equal to `false`? – Felix Kling Sep 20 '17 at 01:53
  • The abstract equality operation in JavaScript is _heuristic_. Read section 7.1.12 (of ES6 standard) to see how it actually works. Beware that "Type( null)" is Null, not Object. The examples posted convert `false` to the number zero for comparison. None of `null`, `undefined` or `NaN` is equal to numeric zero following steps in the algorithm. – traktor Sep 20 '17 at 03:13

1 Answers1

2

The == operator has its own semantics. You're comparing behaviors that are not defined to be the same.

If you want to see how the normal "truthy/falsy" evaluation works, you should use !value or !!value instead of value == false or value == true:

if (!null) console.log("hi");
if (!NaN) console.log("hi");
Pointy
  • 405,095
  • 59
  • 585
  • 614