JavaScript 中正确的将 true false 字符串转为 Boolean 的方法

luoyjx · 2017-07-27 15:31 · 550次阅读

这样:

var isTrueSet = (myValue == 'true');

非必需的:

You could make it stricter by using the identity operator (===), which doesn’t make any implicit type conversions when the compared variables have different types, instead of the equality operator (==), which does:

var isTrueSet = (myValue === 'true');

不可行的:

You should probably be cautious about using these two methods for your specific needs:

var myBool = Boolean("false");  // == true

var myBool = !!"false";  // == true
收藏

暂无评论

登录后可以进行评论。没有账号?马上注册