equals to(==) and exactly equals to(===) operator

/*distinction between ==,=== or !=,!==
In JS, some literals falsy in nature, it means their nature is to prove false:
undefined, null, false, "", 0, or NaN are falsy in nature, rest other types like number(except 0),string
,any object,function,array are truthy in nature.

For example one can check if a variable is undefined(not initialized with any value):
var vbn;
now to check if is not null or undefined:
either one can use if(vbn!=null || vbn != undefined){doSomething}
or simple since null and undefined are falsy in nature above condition can be written like:
if(vbn){doSomething} internally JS will eveluate like:
if(!(undefined||null)) => if(!(falsy||falsy)) = > if(!falsy)

Another example : lets say an object having a property with truthy value (not in undefined, null, false, "", 0, or NaN)
var objj = { propX : "ValX"};
and we want to execute a condition that if propX really exists then do something:
if(objj.propX){doSomething} //which proves to be true because propX property exists
if(objj.propY){doSomething} //which proves to be false because propY property doesn't exists

now say the same object have a property propX with false value:
var objj = { propX : undefined};
and we want to execute a condition that if propX exists and also value is truty then do something:
if(objj.propX){doSomething} //which proves to be false because even though propX property exists
JS will interpret the if condition :
"if propX property exists and also if value of propX is a truthy then doSomething".
Thus it relives us from the pain of having checking the existence of property and value separately.

Another example:

// If o has a property x whose value is not null or undefined, double it.
if (o.x != null) o.x *= 2;

// If o has a property x whose value does not convert to false, double it.
// If x is undefined, null, false, "", 0, or NaN, leave it alone.
if (o.x) o.x *= 2;

now lets see difference between ==,===
==(equals to operator) means what the two literals nature denotes.
For instance the literals null and undefined denotes the nuture of falsehood (void value,nothing present).
Thus the expression if(null==undefined){doSomething} will evaluate to true, becuase both
null and undefined shows the nature of falsy(void value) value.

===(exactly equal to operator) means whats the exact native nature.
For instance the literals null and undefined are introduced to JS for different purpose.
null is a type while undefined is a global property, thus === exactly equals to operator, checks
the native nature of the literasl , thus in this case :
if(null === undefined) will evelaute to false

Also like if(1 == true) will evelauate to true while if(1===true) will not
becuase at native level true is a boolean type while 1 is a number type
*/
if (null == undefined) console.log("we are just equal");
if (null === undefined) {
console.log("we are exactly equal");
}
else {
console.log("we are not exactly equal");
}

if (!(NaN == undefined)) console.log("we are not equal");
//though both are of falsy nature, still both doesn't point to approx value.
//NaN represnts a value which is not a number ,while undefined points to void value, hence the difference.

if(1==true)console.log("we are just equal");
if (1 === true) {
console.log("we are exactly equal");
}
else {
console.log("we are not exactly equal");
}

//!= and !== are like counter parts of above.

Comments

Popular posts from this blog

Custom Module Export

JS prototype

JS Array