Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript logical "!==" operator?

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 ! + ==?

like image 337
Cory Gross Avatar asked Jun 03 '12 15:06

Cory Gross


People also ask

What are the 3 logical operators in JavaScript?

There are four logical operators in JavaScript: || (OR), && (AND), !

What are the 5 logical operators?

There are five logical operator symbols: tilde, dot, wedge, horseshoe, and triple bar. Tilde is the symbol for negation. The word “not” and the phrase “it is not the case that” are used to deny the statement that follows them (we refer to their use as “negation”).

What is the difference between == and === operators in JavaScript?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.


1 Answers

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.

like image 55
Wormbo Avatar answered Sep 19 '22 13:09

Wormbo