Monday, April 8, 2013

=== vs == in Javascript

JavaScript provides two comparison operator strict equality (===) and normal or lenient equality(==)

Strict Equality only compare the value if their types are equal .Values with different type can never be equal.Triple equals do the exact comparison of the actual value.

if(5==="5") //false

if(' '===0) //false

if(3===3) //true

Lenient Equality  compare the value with different type ,then type coercion will occur means if you are comparing string with number then browser will convert string to number before values comparison.Likely for Boolean and int comparison.

if(5=="5") //true

if(0==false) //true

Inequality is  similar as above

if using != operator then because of type coercion below examples are giving unexpected result

if(9!="9") //false

if(0!=false) //false

use !== operator to see the desired output

if(9!=="9")  // true

if(0!==false) // true


5 comments:

  1. Thanks a ton for the help. I also found this very useful link with free online tests for GATE. They are sure to be useful for people. Here is the link: http://thegateacademy.com/gate-free-test/

    ReplyDelete
  2. C'est vraiment bien de lire le contenu de ce blog. Voici un forum généraliste et bien informé connu par ce blog. J'apprécie ce blog avec une telle connaissance pédagogique.
    wordpress
    blogspot
    youtube
    បាការ៉ាត់

    ReplyDelete