0

While writing simple snippet for comparison in JavaScript I observed some weird behavior.

Case 1:

typeof(window.WHTStatement.DDL_TPTypeID.size()) ==> "number"
typeof(window.WHTStatement.Txt_TPTypeValue.size()) ==> "number"

window.WHTStatement.DDL_TPTypeID.size() == 1 == window.WHTStatement.Txt_TPTypeValue.size()

returns true -- OK


Case 2:

window.WHTStatement.DDL_TPTypeID.size() === 1 == window.WHTStatement.Txt_TPTypeValue.size()

returns true -- OK


Case 3:

window.WHTStatement.DDL_TPTypeID.size() === 1 === window.WHTStatement.Txt_TPTypeValue.size()

returns false, why?

What exactly happening here in case 3. Can somebody elaborate?

pimvdb
  • 151,816
  • 78
  • 307
  • 352
Muhammad Shoaib
  • 679
  • 5
  • 6
  • This might help http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use/359509#359509 – Umer Hayat Nov 04 '11 at 07:37
  • It's not "weird" behaviour; there is a difference between "weird" behaviour and behaviour which you -- not anyone else, though! -- did not expect. – Lightness Races in Orbit Nov 04 '11 at 07:38

1 Answers1

4

Unlike Python, in JS x == y == z is not equal to x == y && y == z but (x == y) == z. So you are actually comparing a boolean to a number which obviously fails in a type check.

The == comparison worked because 1 == true is true.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636