the Future of the Web
  • Home
  • Articles
  • Contact
  • Confusing JavaScript Equality

    Jul 25 2007

    I got tripped up today by something that took me a few minutes to figure out. I wrote this:

    if (a == b == 0) {
        // only execute if both a and b are zero
    }
    

    But this was wrong. In fact, you can write this:

    alert(3 == 4 == 0); // alerts "true"
    

    Why is that? Because of the order things are evaluated. I made the mistake of thinking == has the same result as doing =:

    var x, y;
    
    x = y = 10;
    
    alert(x); // 10
    alert(y); // 10
    

    But when you use == like that, it actually compares the firsts two values, then compares the result (true or false) against the 3rd value. It's the same as writing:

    alert(3 == 4 == 0); // true
    alert((3 == 4) == 0); // true
    

    because 3 == 4 is false, and false == 0 is true!

    Tags: javascript
    View 2 Comments | Add a comment
  • Comments

    1. Arjan Eising at 5:35pm on July 25, 2007

    Such things can really give you a headache when you can't find the problem...

    2. Mark at 2:15pm on August 8, 2007

    OMG, it even took me a few moments to get the line:
    "because 3 == 4 is false, and false == 0 is true!"
    LOL

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

    Jesse Skinner
    • About Me
    • Email Me
    • RSS Feed RSS Icon
    • @JesseSkinner
  • Recent Articles

    • jQuery Live Events
    • I need web developers
    • buttons need type="submit" to submit in IE
    • Win $200 in a Web Dev Writing Contest
    • Use Arrays in HTML Form Variables
    • 5 Reasons Freelancers Can Succeed in a Shrinking Economy
    • Keeping a Live Eye on Logs
    • Using PHP's empty() Instead of isset() and count()
    • Testing Web Pages with Lynx
    • Stop CSS Background Flickering in Internet Explorer 6
    • See All...
  • Categories

    • javascript (39)
    • links (21)
    • about (19)
    • web (14)
    • html (12)
    • server (11)
    • css (8)
    • browsers (8)
    • carnival (7)
    • work (6)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
  • Older Articles

    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2010 The Future of the Web