-1

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?

2
  • This might help stackoverflow.com/questions/359494/… Commented Nov 4, 2011 at 7:37
  • It's not "weird" behaviour; there is a difference between "weird" behaviour and behaviour which you -- not anyone else, though! -- did not expect. Commented Nov 4, 2011 at 7:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.