Boolean(“false”) returns true.. any alternative?

前端 未结 2 1902
梦毁少年i
梦毁少年i 2021-01-28 11:58

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 a

相关标签:
2条回答
  • 2021-01-28 12:34

    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.

    0 讨论(0)
  • 2021-01-28 12:45

    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') {
       .....
    }
    
    0 讨论(0)
提交回复
热议问题