Sunday, July 7, 2013

Strict Comparison ‘===’ and ‘!==’ in JavaScript


While working with windows 8 applications, I have encountered “===” and “!==” comparisons many times. At first glance, it looked like odd to me, then I realize that it’s nothing but comparison operation. Apart from normal comparison we do in day to day programming like in C#, with “==”, these operators however allow us to do some strict comparisons.

By strict comparison I mean that the system will not type convert the variables while comparing them. For instance, normally we presume 0 == false as true, what actually happen is this, zero is type converted to Boolean, which is false. Thus false == false will return true. But when we talk about strict comparisons, 0 == false is false. Here zero will not be type converted to Boolean, thus resulting the expression becoming false.

Considering another example,

1 == “1”
1 === “1”

Here the first comparison will results in true as the expression is typed coerced so as to make the comparison of string with the Boolean. Thus will result in true. In the second case however, the type coercion will not happen thus resulting in false as the answer.

Hence it can be summarized easily as follows, “==” convert the operands into a proper type then compare. With “===”, there is no type conversion whatsoever, it compares what it sees.

I hope you’d like the post. Stay tuned.

Happy Reading!!!

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top