• || , && and !!

    && is guard operator aka "logical and", and || is default operator "aka logical or", we normally see the code like the following

    if ( condtion1 && condition2) { }
    if (condition1 || condition2) { }
    

    && is also know as guard operator. If first operand is truthy, the result is second operand, else result is is first operand. It can be used to avoid null reference.

    if (a){
      return a.memeber;
    } else {
       return a;
    }
    //this is the same
    return a && a.member
    

    || is aslo know is default operator. It is the opposite of && . If first operand is truethy, then result is first operand, else result is second operand. It can be used to fill in default value like the following, it is like ?? in csharp


    var last = input || {}; //{} is default value
    


    The !! operator can turn all value into boolean value, you can think of it boolean conversion operator. Keep in mind that truthy/falsy value is not boolean value. For example "hello" is truthy value. So you can write code like


    if ("hello")
    {
       alert("hello");
    }
    


    But "hello" itself is not boolean value, if you want to convert it into boolean value, you need to write


    function toBoolean(v)
    {
       if (v)
       {
         return true;
       }
       else
       {
          return false;
       }
    }
    
    
  • test

    test

  • context of setTimeout and eval

    setTimeout is a method of window object. “this” context refer to the window. But you can change its context in the following way. setTimeout does not support call method.

    setTimeout(function() { alert(this); }, 0); //window
    //setTimeout.call({}, function() { alert(this); }, 0); //not supported
    setTimeout((function() { alert(this); }).call({}), 0); //object 
    
  • Anonymous function's contexgt

    var name = "window";
        var f = (function() { alert(this.name); });
        f(); //window
        f.name = "local";
        f();     //still window, the function still run in the context of window
        f.call(f); //local
    
    
  • == vs ===

    Here is the msdn documentation

    Equality (==, !=)

    • If the types of the two expressions are different, attempt to convert them to string, number, or Boolean.
    • NaN is not equal to anything including itself.
    • Negative zero equals positive zero.
    • null equals both null and undefined.
    • Values are considered equal if they are identical strings, numerically equivalent numbers, the same object, identical Boolean values, or (if different types) they can be coerced into one of these situations.
    • Every other comparison is considered unequal.

    Identity (===, !==)

    These operators behave identically to the equality operators except no type conversion is done, and the types must be the same to be considered equal.

    Here is some test case writing in Qunit.

    test("Equality test", function() {
        ok(1 == 1 && 'a' == 'a' && 1 == '1' && 0 == false && '' == false);
        ok(null == null, "null equals to null");
        ok(null == undefined, "null equals undefined");
        ok(undefined == undefined, "undefined equals undefined");
        ok({} != {}, "different objects are unequal");
    });