How do I compare string and boolean in Javascript?

后端 未结 9 1248
长情又很酷
长情又很酷 2020-12-11 01:16

I got the Json \"false\" from server. I respond as bool but it\'s Json so it\'s in browser type is String instead of bool

9条回答
  •  天涯浪人
    2020-12-11 01:46

    I would just explicitly check for the string "true".

    let data = value === "true";
    

    Otherwise you could use JSON.parse() to convert it to a native JavaScript value, but it's a lot of overhead if you know it's only the strings "true" or "false" you will receive.

提交回复
热议问题