Javascript logical "!==" operator?

Cory Gross picture Cory Gross · Jun 3, 2012 · Viewed 143.4k times · Source

I am getting back into web development, and have been trying to go over the nuances of jscript recently. I was pouring through the source of the THREEx extension library built on top of Three.JS and noticed this function

THREEx.KeyboardState.prototype.pressed  = function(keyDesc)
{
    var keys    = keyDesc.split("+");
    for(var i = 0; i < keys.length; i++){
        var key     = keys[i];
        var pressed;
        if( THREEx.KeyboardState.MODIFIERS.indexOf( key ) !== -1 ){
            pressed = this.modifiers[key];
        }else if( Object.keys(THREEx.KeyboardState.ALIAS).indexOf( key ) != -1 ){
            pressed = this.keyCodes[ THREEx.KeyboardState.ALIAS[key] ];
        }else {
            pressed = this.keyCodes[key.toUpperCase().charCodeAt(0)];
        }
        if( !pressed)   return false;
    };
    return true;
}

I am looking in particular at the line here:

if( THREEx.KeyboardState.MODIFIERS.indexOf( key ) !== -1 ){

I am not familiar with this !== operator. I checked w3schools and their logical operators list does not have this one included. I am not sure if this is misspelled and the browsers simply count it as != or if it has some other meaning. Also I was wondering whether this is actually a single logical operator or whether it is some kind of combination, like ! + ==?

Answer

Wormbo picture Wormbo · Jun 3, 2012

You can find === and !== operators in several other dynamically-typed languages as well. It always means that the two values are not only compared by their "implied" value (i.e. either or both values might get converted to make them comparable), but also by their original type.

That basically means that if 0 == "0" returns true, 0 === "0" will return false because you are comparing a number and a string. Similarly, while 0 != "0" returns false, 0 !== "0" returns true.