Skip to content Skip to sidebar Skip to footer

Boolean("false") Returns True.. Any Alternative?

I am using jquery to write true and false strings to data- html attributes. However when I write false to one of the attributes, and then check it it false, it returns true. I also

Solution 1:

Boolean('false') will return true because the string 'false' is truthy.

You could just run a check to see if the string equals 'false':

if ($(this).attr("data-is-panel-activated") === 'false') {
   .....
}

Solution 2:

The simplest fix would be:

var is_activate = $(this).attr("data-is-panel-activated") !== "false";

or

var is_activate = $(this).attr("data-is-panel-activated") === "true";

depending on which semantics makes more sense for your code.


Post a Comment for "Boolean("false") Returns True.. Any Alternative?"