True but not true
Written by Ian Elliot   
Article Index
True but not true
Solution

Solution

The key to the problem is to realize that not all things that are true in JavaScript are equal to true.

The original programmer of the code had something slightly different in mind to flag being a Boolean. They were working with strings and discovered that if flag was "true" or any non-null string then

flag="true";
if(flag)

evaluated to true. On the other hand if flag was the null string then

flag="";
if(flag)

evaluated to false. This worked perfectly in this situation as the test was about what to do when the string was null or not. 

JavaScript allows any type to be treated as if it was a Boolean - so called truthy and falsey values.  Any zero, null, undefined or null string are falsey and behave in logical expressions as if they were false and everything else is truthy and behave in logical expressions as if they were true.

However our refactoring programmer was unaware of the intent and assumed that flag was simply a Boolean and true or false rather than truthy or falsy.

So why didn't ==true or ===true work if flag was truthy?

The simple answer is that for == JavaScript attempts an automatic conversion. The way that this works is that to evaluate  x==y any Booleans are first converted to numbers with true as 1 and false as 0. So flag==true is converted to flag==1 which it isn't so the result is false. 

For the strict equality operator the result is even simpler. If the two values are of different types then the result is false - and flag is a string and true is a Boolean.

Pattern

The pattern that you need to follow to make sure that hoisting never causes you any problem is simple:

Don't ever use or rely on truthy and false values.

Always use a Boolean if you need a two state indicator.

This said there are problems even in this approach as there are two distinct ways to create Boolean as:

var b=new Boolean(true);

and

var b="true";

The bad news is that these two don't always behave in the same way - but that's for another Programmer Puzzle.

For more on this topic:

Javascript Jems - active logic, truthy and falsey

 

Banner

More Puzzles

Sharpen Your Coding Skills
Towers Of Hanoi Mutants

Towers of Hanoi is a classic puzzle and is often used to illustrate the idea of recursion. Here Melvin Frammis challenges you to attempt to find solutions to some variations - but first he explains th [ ... ]


Javascript
JavaScript Puzzle - The Too Tidy Assignment

There is a rule about not optimizing code too soon. Perhaps we should add to that the rule that being too tidy is often a mistake. Here we have a JavaScript example where things go wrong because of an [ ... ]


Sharpen Your Coding Skills
The Best Sub-Array Problem

At first glance this puzzle seems trivial, all you have to do is find a sub-array, in an array of numbers,  that sums to the largest value. It sounds almost too easy to need a solution, let alone [ ... ]


Other Articles



<ASIN:059680279X>

<ASIN:0470526912>

<ASIN:1590597273>

<ASIN:0596806752>



 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.