gpt4 book ai didi

javascript - 使用 boolean 类型与字符串类型 "true"/"false"的优缺点是什么?

转载 作者:行者123 更新时间:2023-11-30 10:16:30 24 4
gpt4 key购买 nike

在Javascript中,我看到过使用字符串类型来表示“true”和“false”的代码。为什么不简单地使用 boolean 类型呢?

在 Javascript 中使用 boolean 类型与字符串类型“true”/“false”的优缺点是什么?还是使用字符串类型来表示 boolean 值只是一种不好的做法?

最佳答案

字符串 "true""false" 被认为是真实的。因此,切勿使用它们代替 boolean 值。

console.log(Boolean("true"));
# true
console.log(Boolean("false"));
# true

还有,

console.log(true  != "true");
# true
console.log(false != "false");
# true

关于javascript - 使用 boolean 类型与字符串类型 "true"/"false"的优缺点是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23404028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com