=== is called Identity Operator compares value AND type.
== will do type conversion. Namely, it converts the type down to a comparable value and match that way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
var strVar = "Ricky"; var strVar1 = "6680"; var strVar2 = "Ricky"; var strVar3 = ""; var intVar = 6680; var nanVar = NaN; var nullVar = null; var undefinedVar = undefined; console.log('------------------- STRING AND INT types ---------------------'); // typeof Ricky is string console.log('typeof ' + strVar + ' is ' + typeof(strVar)); // typeof 6680 is number console.log('typeof ' + intVar + ' is ' + typeof(intVar)); // typeof NaN is number console.log('typeof ' + nanVar + ' is ' + typeof(nanVar)); // typeof null is object console.log('typeof ' + nullVar + ' is ' + typeof(nullVar)); // typeof undefined is undefined console.log('typeof ' + undefinedVar + ' is ' + typeof(undefinedVar)); // "Ricky" + 6680 = "Ricky6680" var mixedVar = strVar + intVar; console.log(`1) 'Ricky' + 6680 = '${mixedVar}', which is of type: ${typeof(mixedVar)}`); // "Ricky" * 6680 = Nan var mixedVar2 = strVar * intVar; console.log(mixedVar2) // 2 * "2" = 4 var mixedVar3 = 2 * "2"; console.log(`2) 2 * '2' = ${mixedVar3}, type ${typeof(mixedVar3)}`) var bVar = true; // true + "Ricky" = "trueRicky" var mixedVar2 = bVar + strVar; console.log(`3) true + "Ricky" = '${mixedVar2}' which is of type: ${typeof(mixedVar2)}`); //true + 10 = 11 var mixedVar3 = bVar + 10; console.log(`4) true + 10 = ${mixedVar3} which is of type: ${typeof(mixedVar3)}`); //"Ricky" === "Ricky" if(strVar === strVar2) { console.log( typeof(strVar) + ' ' + strVar + ', and ' + typeof(strVar2) + ' ' + strVar2 + ', are ===' ); } //"Ricky" !== "6680" if(strVar !== strVar1) { console.log( typeof(strVar) + ' ' + strVar + ', and ' + typeof(strVar1) + ' ' + strVar1 + ', are !==' ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
var boolTrueVar = true; var boolFalseVar = false; console.log("-------------- boolean types -------------------"); // typeof true is boolean console.log('typeof ' + boolTrueVar + ' is ' + typeof(boolTrueVar)); // typeof false is boolean console.log('typeof ' + boolFalseVar + ' is ' + typeof(boolFalseVar)); // true === true if(boolTrueVar === boolTrueVar) { console.log( typeof(boolTrueVar) + ' ' + boolTrueVar + ', and ' + typeof(boolTrueVar) + ' ' + boolTrueVar + ', are ===' ); // true + true = 2 let ans = boolTrueVar + boolTrueVar console.log(` true + true = ${ans}`) } // true !== "true", their types do not match if(boolTrueVar !== "true") { console.log( typeof(boolTrueVar) + ' ' + boolTrueVar + ', and ' + typeof("true") + ' ' + "true" + ', are !==' ); } // true !== false if(boolTrueVar !== boolFalseVar) { console.log( typeof(boolTrueVar) + ' ' + boolTrueVar + ', and ' + typeof(boolFalseVar) + ' ' + boolFalseVar + ', are !==' ); // true + false = 1 + 0 = 1 let ans = boolTrueVar + boolFalseVar console.log(` true + false = ${ans}`) } // true == 1 if(boolTrueVar == 1) { console.log( typeof(boolTrueVar) + ' ' + boolTrueVar + ', and ' + typeof(1) + ' ' + 1 + ', are ==' ); // true + 1 = 2 let ans = boolTrueVar+1; console.log(`${boolTrueVar} + 1 = ${ans}, type is: ${typeof(ans)}`) } // true == "1" if(boolTrueVar == "1") { console.log( typeof(boolTrueVar) + ' ' + boolTrueVar + ', and ' + typeof("1") + ' ' + "1" + ', are ==' ); // true + '1' = 'true1' let ans = boolTrueVar+"1" console.log(`true + '1' = ${ans}, type is: ${typeof(ans)}`) } |
Notice that true != “true”, I want to comment that by rule 5, since true is Boolean, it gets converted into a number.
Hence, 1 != “true” is true.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// true != 'hello' if(boolTrueVar != "hello") { console.log( typeof(boolTrueVar) + ' ' + boolTrueVar + ', and ' + typeof("hello") + ' ' + "hello" + ', are !=' ); // true + 'hello' = 'truehello' let ans = boolTrueVar + "hello"; console.log(`true + 'hello' = '${ans}', type ${typeof(ans)}`) } // true != 'true' if(boolTrueVar != "true") { console.log( typeof(boolTrueVar) + ' ' + boolTrueVar + ', and ' + typeof("true") + ' ' + "true" + ', are !=' ); // true + 'true' = 'truetrue' let ans = boolTrueVar + "true"; console.log(`true + 'true' = ${ans}, type ${typeof(ans)}`) // true * 'true' = Nan let ans2 = boolTrueVar * "true"; console.log(`true * 'true = '${ans2}, type ${typeof(ans)}`) } // true != 5 if(boolTrueVar != 5) { console.log( typeof(boolTrueVar) + ' ' + boolTrueVar + ', and ' + typeof(5) + ' ' + 5 + ', are !=' ); // true + 5 = 1 + 5 = 6 let ans = boolTrueVar + 5 console.log(`true + 5 = ${ans}, type ${typeof(ans)}`) } // false === false if(boolFalseVar === boolFalseVar) { console.log( typeof(boolFalseVar) + ' ' + boolFalseVar + ', and ' + typeof(boolFalseVar) + ' ' + boolFalseVar + ', are ===' ); } |
Similarly, remember that in “==”, from rule 5), booleans are converted to numbers first.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// typeof true is boolean console.log('typeof ' + boolTrueVar + ' is ' + typeof(boolTrueVar)); // typeof false is boolean console.log('typeof ' + boolFalseVar + ' is ' + typeof(boolFalseVar)); // false == 0, 0 == 0 if(boolFalseVar == "0") { console.log( typeof(boolFalseVar) + ' ' + boolFalseVar + ', and ' + typeof("0") + ' ' + "0" + ', are ==' ); } // false != "false", 0 != "false' if(boolFalseVar != "false") { console.log( typeof(boolFalseVar) + ' ' + boolFalseVar + ', and ' + typeof("false") + ' ' + "false" + ', are !=' ); } // false !== "false", type mismatch if(boolFalseVar !== "false") { console.log( typeof(boolFalseVar) + ' ' + boolFalseVar + ', and ' + typeof("false") + ' ' + "false" + ', are !==' ); } // false != "hehe", 0 != "hehe" if(boolFalseVar != "hehe") { console.log( typeof(boolFalseVar) + ' ' + boolFalseVar + ', and ' + typeof("hehe") + ' ' + "hehe" + ', are !=' ); } |
Null manipulation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
console.log('-------------- NULL UNDEFINED TYPES ---------------'); var nullVar = null; var unDefVar = undefined; // null is object console.log('typeof ' + nullVar + ' is ' + typeof(nullVar)); console.dir(nullVar); // null // typeof undefined is undefined console.log('typeof ' + unDefVar + ' is ' + typeof(unDefVar)); // null + null is: 0, which is of type: number console.log('null + null is: ' + (nullVar + nullVar) + ', which is of type: ' + typeof(nullVar + nullVar)); // null + 1 is: 1, which is of type: number console.log('null + 1 is: ' + (nullVar + 1) + ', which is of type: ' + typeof(nullVar + 1)); // null + abc is: nullabc, which is of type: string console.log('null + abc is: ' + (nullVar + 'abc') + ', which is of type: ' + typeof(nullVar + 'abc')); |