Tuesday, December 3, 2013

Javascript Identity Operator vs Equality Operator

 

Equality and inequality tests

==, !=
It checks and compare values of the variable.
 

Example

var firstVal = 5;//Number

var secondVal = "5";//String

if (firstVal == secondVal) {

console.log("They are the equal");

} else {

console.log("They are NOT the equal");

}

Output:They are the equal
 ONlY Values are compared.

 

Identity and nonidentity tests

===, !== It checks and compare values and types of the variable.

Example

var firstVal = 5; //Number

var secondVal = "5"; //String

if (firstVal === secondVal) {

console.log("They are the same");

} else {

console.log("They are NOT the same");

}

Output : They are  NOT the same.

Values and Types are compared.
 

No comments :