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 ! + ==?
There are four logical operators in JavaScript: || (OR), && (AND), !
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”).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With