Geeks With Blogs

  • BrianSchroer I see a lot of fans wearing #stlcards red at the Cincinnati ballpark. ;) about 681 days ago
  • BrianSchroer What part of "What part of x do you not understand" do you think is clever? about 681 days ago
  • BrianSchroer Anonymous notes to those with whom I shared a conference call today: 1) The "t" is silent in "across". 2) "Uniquer" is not a word. about 684 days ago
  • BrianSchroer KSDK: "Don't take any chances. Get in the basement now. Now let's hear from our reporter, driving up I-64 TOWARD THE STORM." about 689 days ago
  • BrianSchroer Congratulations to Joanna Schroer & University City High School Class of '13 #UTime about 690 days ago
  • BrianSchroer Ugh. McCarver doing the #stlcards game. This guy makes Hrabosky seem witty and concise by comparison. about 695 days ago


Brian Schroer Don't Call Me Mort!
Technorati Tags:


Did you know that there are two equality operators in JavaScript?

The one that's used most often is the double equal sign ("==") operator. The "==" operator does type coercion. The following example returns true, even though we're comparing a string and a number:

    if ('5' == 5)

That's not too bad, but you probably wouldn't expect the following examples to return true:

    if (0 == '')

    if (0 == false)

    if (false == undefined)

    if ("\r\n" == 0)

They all do though, thanks to JavaScript's concept of "truthiness".

The lesser-known JavaScript equality operator is the triple equal sign strict equality operator ("==="). It checks that values are the same value and the same type, so these all return false:

    if ('5' === 5)

    if (0 === '')

    if (0 === false)

    if (false === undefined)

    if ("\r\n" === 0)

(The corresponding strict inequality operator is "!==").

Douglas Crockford, author of "JavaScript: The Good Parts", recommends always using the strict equality operators, and I recommend always following the WWDD (What Would Doug Do?) principle when it comes to JavaScript.

You can watch a great Douglas Crockford presentation about the good, the bad, and the ugly of JavaScript here:

Posted on Saturday, July 3, 2010 7:11 AM JavaScript | Back to top

Comments on this post: Quality Equality with JavaScript: "===" > "=="

# re: Quality Equality with JavaScript: "===" > "=="
Requesting Gravatar...
Thanks this really cleared triple equal sign up for me.
Left by RedriderX on Oct 04, 2011 9:05 AM

Your comment:
 (will show your gravatar)

Copyright © Brian Schroer | Powered by: | Join free