如何比较在Javascript中的字符串和布尔? [英] How do I compare string and boolean in Javascript?

查看:127
本文介绍了如何比较在Javascript中的字符串和布尔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了JSON的从服务器。我回应为布尔,但它的Json因此它在浏览器类型为字符串而不是 BOOL

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.

所以,如果我跑(!数据)每当我要检查假==假然后他们没有奏效。

So if I run (!data) whenever I want to check "false" == false then they not worked.

那么,如何可以解析布尔字符串在JavaScript中呢?

So how can I parse bool from String in JavaScript then?

真实==真假==假。然后,code (!数据)可以检查它是什么[真正]

"true" == true and "false" == false. Then the code (!data) can check what it is [true and false]

推荐答案

使用 JSON.parse()来 (如果您的浏览器支持,如果没有,下载一个库)将其转换为一个本地JavaScript值。

Use JSON.parse() (if your browser supports it, if not, download a library) to convert it to a native JavaScript value.

否则,该字符串expliclty检查

Otherwise, expliclty check for the string "false".

这篇关于如何比较在Javascript中的字符串和布尔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆