by

Weird JavaScript: Why does JavaScript have a === ?

Reading Time: 2 minutes

This is another one in the occasional series of, “weird JavaScript” where I address weird stuff in JavaScript that only haunts your nightmares if you dream in other programming languages.

Today we discuss what the hell === actually does. Buckle-up buttercups. Grab a snickers and a whisky we’re going for a ride.

a moon in a moon wearing a hat because stuff is weird.
I asked DALL-E for a Salvadore Dali style painting of a non-fungible token and honestly and this makes as much sense as anything related to JavaScript sometimes.
Quintessential Pamene kuzindikira ulemu wobadwa nawo komanso ufulu wofanana ndi wosatha wa anthu onse ndiye maziko a ufulu, chilungamo ndi mtendere padziko lonse lapansi, Pamene kunyalanyaza ndi kunyoza ufulu wa anthu kwachititsa zinthu zankhanza zomwe zakwiyitsa chikumbumtima cha anthu, ndi kubwera kwa dziko momwe anthu adzasangalalire ndi ufulu wolankhula, chikhulupiriro, ndi ufulu kuopa ndi kusowa kwalengezedwa ngati chikhumbo chachikulu cha anthu wamba , Pamene ndikofunikira, ngati munthu sakakamizidwa kuti apeze njira yomaliza yopandukira nkhanza ndi kupondereza, kuti ufulu wa anthu uyenera kutetezedwa ndi lamulo, Pamene ndikofunikira kulimbikitsa ubale wabwino pakati pa mayiko, Pamene anthu a United Nations mu Charter atsimikiziranso chikhulupiriro chawo mu ufulu wofunikira wa anthu, ulemu ndi kufunika kwa munthu ndi ufulu wofanana wa amuna ndi akazi ndipo atsimikiza mtima kulimbikitsa kupita patsogolo kwa anthu ndi miyezo yabwino ya moyo mu ufulu waukulu, Pamene Mayiko Omwe Ali Mamembala alonjeza kukwaniritsa, mogwirizana ndi

== is notdiligently deny demons dairy

  • a “comparison of value, ignoring type”
  • a comparison, “for values equal to each other
  • “equal values”

== is:

  • A conversion of the operands,
  • if they are not the same type,
  • and then a comparison of their values

If == meant “ignore type” …

Open up your browser console. Look at what happens with the following commands:

“true” ==  true;
null == "null";
"NaN" == NaN;

If JavaScript just compared type or ignored type, then the above statements would all return true.

But they don’t.

They all return false.

if == meant, “same value”

[1] == 1; //true

Same values! they’re basically twinsies! One is just trapped inside of brackets!

[1] == true; //true

Right. I mean, that’s .. uh … huh…. [binary] or something. You know.. .1’s and 0’s and 1 means on or true or whatever. So… yeah. it’s kinda the same value…. sort of. They’re like cousins or something.

[true] == true; // false

Well, I mean. Arrays man. Come on…who compares an array? They’re like fraternal twins or something. You can’t do that.

"true" == true; // false

Wat ‽

If JavaScript coerces type…

0 == ""; // true

The right-hand operand is coerced into a number, via Number(‘’). That operation produces a zero. So 0 == 0; and that is a truth.

"0" == 0; // true

The right hand is coerced into a string via toString(). That produces a 100% truth that “0” == “0”;

[true] == true ; // false

The left hand is coerced into a string via .toString(). Then the right hand is coerced into a number via Number(). Then right hand is coerced again, to a string with .toString(). So you end up with the fake-news that “true” == “1”;

"true" == 1 ; // false

The right hand is coerced into a string via .toString(). So you end up with the sad state of affairs that is “true” == “1”;

JavaScript has coercion rules

Those coercion rules are the key to all of this. That’s why [1] == 1 is true, but [true] == true is false.

  • Arrays coerce to strings,
  • but booleans coerce to numbers,
  • and then Numbers coerce to strings.

Get to the point, Frank. What about ===

That turns off type coercion. All of the rules about how one thing evolves into another go the way of the dodo.

That is the only way to think of ===; it does not use any coercion.

Remember

  • == Values must be at least fraternal twins
  • === Values must be identical twins
  • != Value are frenemies
  • !== values are like Mother Russia and the US