“Triple Equals” => Secret JavaScript Kung Fu Revelation!

I got really psyched about an online wordgame called DeepLeap recently — I think it does a fantastic job of requiring and encouraging intelligence (at least, lexical depth, creativity, and swift thinking) — so I looked through the JavaScript code it’s written in to understand it better, where I discovered this intriguing operator, “===”.

Triple equals?  A typo?

No!  Turns out that’s a real thing!  And it’s awesome and hella useful to know about!

It’s a test for equality AND type congruence — for example:

1 == “1” is true, because they’re both equal to one, but

1 === “1” is false, because they’re not the same data type (one’s an integer, the other’s a string).

Additionally, in searching for this knowledge, I found a blog post which led me to the YUI theater — Yahoo has talks online about JavaScript development.  Hot knowledge!!!  You can learn a lot about coding and design by watching the talks posted there.

 

[1] DeepLeap
[2] from this code line:

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
Advertisements

5 Comments

Filed under Uncategorized

5 responses to ““Triple Equals” => Secret JavaScript Kung Fu Revelation!

  1. Burp

    Yes, so what ? This is standard stuff defined in the ECMAScript spec.

    So that you know, there is even !== for !(===), how incredible.

    What’s the secret ? Where’s the kung fu ?

    • boranj

      I share things as I learn them, because I think they’re nifty and interesting. Maybe someone else will learn about it by reading my blog, and benefit too.

      Congratulations on being ahead in the knowledge game.

  2. It’s not quite just that — it tests for identity, rather than just equality. So it’s `eq`, rather than `eqv` or `equal`; or `==` in C or Java, rather than Java’s `.equals()`; or `is` rather than `==` in Python. As a practical consequence, `[]===[]` is `false`.

    • boranj

      ah wow, that’s a very subtle nuance. Very cool! Thanks, Kragen!

      • I just happened to look it up in ECMA-262, and I was wrong! You were right in the first place! The definitions of `==` and `===` differ only in their behavior when the operands are of different types. Furthermore, `==` has such crazy coercion rules that it’s nearly always better to use `===` — Doug Crockford recommends against ever using `==`!