As with all our puzzles this one has been stripped down to its essentials. In the real code the situation was much more complicated and it was much harder to see what was going on.
In this case the problem all started with a little refactoring - as it often does. The original code was a simple if with two options - represented here by alerts:
The setting of the flag variable, clearly expected to be a Boolean was in the test case true as proved by the fact that the alert box popped up and displayed "true".
For reasons of aesthetics the programmer in question didn't like the idiom
The change was duly made to give:
All should be good but the same test case applied to the code produced an alert box which displayed "false".
Immediately the suspicion fell on the use of the == inexact equality operator but when the program was changed to
it produced the same behavior.
What is going on?
How can if(flag) be true and if(flag===true) be false?
This seems like an odd way to do logic!
Turn to the next page when you are ready to find out.