如何比较在Javascript中的字符串和布尔? [英] How do I compare string and boolean in 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屋!