Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
Example as:
if (c === 0){
//
}
What is the meaning of === here in above ex?
Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
Example as:
if (c === 0){
//
}
What is the meaning of === here in above ex?
It checks that c
is equal to the number 0
. ===
is the strict equality operator. It does not attempt to type coerce the operands.
For example:
0 == false; //true (false coerces to 0)
0 === false; //false (no type coercion)
a == b
means that a
equals b
a === b
means that a
equals b
and their types are the same
This is the strict equal operator and only returns a Boolean true if both the operands are equal and of the same type. Assume these:
a = 2
b = 4
These next examples return true:
a === 2
b === 4
There is also a reverse of this operator: !== This is the strict not equal operator and only returns a value of true if both the operands are not equal and/or not of the same type. The following examples return a Boolean true:
a !== b
a !== "2"
4 !== '4'
All quoted from here: http://www.devguru.com/technologies/ecmascript/quickref/comparison_operators.html
Here is a sample
<script type="text/javascript">
var y = 0;
if(y == "0"){
document.write("== '0' True <br/>");
}
else{
document.write("== '0' False <br/>");
}
if(y == 0){
document.write("== 0 Number is True <br/>");
}
else{
document.write("== 0 Number False <br/>");
}
if( y === 0){
document.write("=== 0 Number is True <br/>");
}
else{
document.write("=== 0 Number is False <br/>");
}
if(y === "0"){
document.write("=== 0 is True <br/>");
}
else{
document.write("=== 0 is False<br/>");
}
</script>
If the right value is 0
, you will get
== '0' True
== 0 Number is True
=== 0 Number is True
=== 0 is False
The ==
operator only checks for equivalence of two values while the ===
operator goes an additional step and also asserts that the types of the two values are the same.
2 == "2" // true
While:
2 === "2" // false