0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pathetic Learner
  • 729
  • 1
  • 11
  • 21
  • 1
    http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use – Hadas Feb 07 '12 at 07:53
  • http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use – JKing Feb 07 '12 at 07:54

5 Answers5

2

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)
James Allardice
  • 164,175
  • 21
  • 332
  • 312
2

a == b means that a equals b

a === b means that a equals b and their types are the same

1

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

Samet Atdag
  • 982
  • 6
  • 21
0

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
shenhengbin
  • 4,236
  • 1
  • 24
  • 33
0

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
Ates Goral
  • 137,716
  • 26
  • 137
  • 190