Search code examples
javascriptevaluate

What values are evaluated to true and false without strict comparasion?


I want to have a list of all values that will be evaluated to true or false with poor equality check like !=, == or even if()

I had one list that I collected throught years, but it died alog with my PC


Solution

  • http://bonsaiden.github.com/JavaScript-Garden/#types.equality

    "It's better to learn the coercion rules rather than try to memorize the results of god knows how many different comparisons"...

    ...But here's a list ;)

    ==

    ""           ==   "0"           // false
    0            ==   ""            // true
    0            ==   "0"           // true
    false        ==   "false"       // false
    false        ==   "0"           // true
    false        ==   undefined     // false
    false        ==   null          // false
    null         ==   undefined     // true
    " \t\r\n"    ==   0             // true
    

    ===

    ""           ===   "0"           // false
    0            ===   ""            // false
    0            ===   "0"           // false
    false        ===   "false"       // false
    false        ===   "0"           // false
    false        ===   undefined     // false
    false        ===   null          // false
    null         ===   undefined     // false
    " \t\r\n"    ===   0             // false
    

    Objects

    {}                 === {};       // false
    new String('foo')  === 'foo';    // false
    new Number(10)     === 10;       // false
    
    var foo = {};
    foo                === foo;      // true
    
    NaN                ==   NaN      // false
    NaN                ===  NaN      // false
    NaN                ==   false    // false
    
    //NaN does not coerce using non-strict equality.
    

    Update 29/01/14:

    Added NaN for completeness.