Saturday 1 September 2007

Triple equals in JavaScript

I think I'm a pretty decent javascript programmer, so it's always refreshing to learn something new in a language I'm so familiar with! I recently had some time to watch those great lectures from Doug Crockford at YUI Theatre, and noticed lots of === in the code.

That's right, it's not a typo, but 3 equal signs, and it means equality without type coersion. In other words, if using the triple equals, the values must be equal in type as well.

e.g.
0==false   // true
0===false  // false, because they are of a different type
1=="1"     // true, auto type coersion
1==="1"    // false, because they are of a different type

Another handy tool in my javascript bag of tricks, as recommended by Mr father of javascript himself!