Javascript and its !! (double exclamation mark) opertaor

Let's first see a code example that has !! operator turned on 

 

var store = { 
  id : 1 ,
  cache : {},
  add : function(fn) {
    if (!fn.id) { 
      fn.id = store.id++;
      return !!(store.cache[fn.id] = fn);
    }
  }
};

function ninja() {}

assert (store.add(ninja), "Function was safely added.");
assert(store.add(ninja), "But it was only added once.");

 

 

you may wonder what is the operator of !!...

 

the explanation of the !! operator is such, "!! construct is a simple way of turning any JavaScript expression into its boolean equivalent, for example !!"hello" === true and !!0 === false.  "

 

 

 

In the above example, we converted a function ( which will always be true ) into its boolean equivalent: true.

你可能感兴趣的:(JavaScript)