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"... :)

Comments

Popular Posts

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

Silent Renew and the "login_required" Error When Using oidc-client

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4

How to Determine if a Column Exists in a DataReader