Beware JavaScript's Boolean() Function
I recently wrote the following line of code:
newsletterPopupEnabled = Boolean($("#newsletterPopupEnabled").val());
The point of this was to correctly parse a string of "true" or "false", as well as to take into account an empty string (which I expected to be interpreted as false).
I was very wrong.
The Boolean() function does not parse a string, but simply tests the truthiness of the parameter passed to it. So, a string of "false" is interpreted as truthy because the string is not null.
I now go to ponder the true meaning of "truth"... :)
newsletterPopupEnabled = Boolean($("#newsletterPopupEnabled").val());
The point of this was to correctly parse a string of "true" or "false", as well as to take into account an empty string (which I expected to be interpreted as false).
I was very wrong.
The Boolean() function does not parse a string, but simply tests the truthiness of the parameter passed to it. So, a string of "false" is interpreted as truthy because the string is not null.
I now go to ponder the true meaning of "truth"... :)
Comments
Post a Comment