12 March 2011

What is differenece between == and ===

Creators of  Javascript*  decided that it will be really funny to blow up simplicity of equal operator and they decided to  make  this much more complicated , to be sure that it  will cause lots of confusion when people makes logical assumtion on something that behave ambiguously .So yes ... null , undefined  are just top of the mountain of hell related with comparison in Javascript.

Here we go again,let's clean up this mess.

So....we have 2 types  equality operators
==   called equality operator
=== called identity operator

Most important to remember is that equality and identity operators are checks is both value are equals ,but identity operator checks more strictly (without using  autoconversion so it means both value need to be of the same type (except NaN)

 Equality operator checks
  • for  strings, numbers and booleans
both variable are equal (after automatic conversion)in terms of value  .
For example
1 == "1" will return true
1 == true  will return true (Because boolean can represent true as 1 an false as 0)
  • For arrays  and object 
 are equality operator checks both variable are equal (after automatic conversion)in terms of reference 
(It means checking do they refer to the same array or object,so i)

Identity operator checks
  •  not only values as Equality, doesn't use automatic conversion ,so both values have the same value and the same type (if not are not equal) ,so 
which cause that results are different in some cases (in comaparison to equality operator)
1 === "1" returns false (diffrenet type)
null === null ( returns true) and  undefined ===  undefined ( returns true) BUT null === undefined (returns .... false (???))
 even better because NaN is not equal to NaN


In equals and identity operatos are not identical if they refer to different  array or objects (even if both contain the same  elements



So remember:
=     means "assign to"
==   means "equal to"
=== means "identical to"

1 ==/=== "1" will return true for equality operator and return false for identity operator because comparison has different type (it applied to 1 == true as well )

NaN is always  not equal to NaN for equality operator and identity operator but null ==/=== undefined will return true for equality operator and return false for identity operator

and on end remember that: 
for =
x = 5
means 5 is assign to variable x

for ==
x == 5
means 5 is equal to x (return will answer on question "Is 5 is equal to x?")

for ===
x === 5
means 5 is identical to x (return will answer on question "Is 5 is identical to x?")

If your comparison doesn't work in Java then can be 3 cases:
1)  you mispell something in your code
2) you forgot about one of rule written in this post (about difference between == and ===  and result which will return
3) you forgot about difference between null undefined and NaN (null and undefined issues are explained in previous note

*Javascript is like other languages with some  freaks or i usually said that javascript is a language created by Monty Python.