判断是否为布尔值的方法

wufei123 2024-06-02 阅读:10 评论:0
Boolean() 构造函数可用于创建布尔对象以及布尔原始值,表示 true 或 false 值。 在下面的代码中,我详细介绍了 JavaScript 中布尔值的创建。 示例:sample52.html <!DOCTYPE h...

判断是否为布尔值的方法

Boolean() 构造函数可用于创建布尔对象以及布尔原始值,表示 true 或 false 值。

在下面的代码中,我详细介绍了 JavaScript 中布尔值的创建。

示例:sample52.html

<!DOCTYPE html><html lang="en"><body><script> // Create a Boolean object using the new keyword and the Boolean() constructor. var myBoolean1 = new Boolean(false); // Using new keyword. console.log(typeof myBoolean1); // Logs 'object'. // Create a Boolean literal/primitive by directly using the number constructor without new. var myBoolean2 = Boolean(0); // Without new keyword. console.log(typeof myBoolean2); // Logs 'boolean'. // Create Boolean literal/primitive (constructor leveraged behind the scenes). var myBoolean3 = false; console.log(typeof myBoolean3); // Logs 'boolean'. console.log(myBoolean1, myBoolean2, myBoolean3); // Logs false false false. </script></body></html> Boolean() 参数

Boolean() 构造函数将一个参数转换为布尔值(即 true 或 false)。任何非 0、-0、null、false、NaN、undefined 或空字符串 ("") 的有效 JavaScript 值都将转换为 true。在以下示例中,我们创建两个布尔对象值:一个 true 和一个 false。

示例:sample53.html

<!DOCTYPE html><html lang="en"><body><script> // Parameter passed to Boolean() = 0 = false, thus foo = false var foo = new Boolean(0) console.log(foo); // Parameter passed to Boolean() = Math = true, thus bar = true var bar = new Boolean(Math) console.log(bar); </script></body></html>

当与 new 关键字一起使用时,来自 Boolean() 构造函数的实例会生成一个实际的复杂对象。您应该避免使用 Boolean() 构造函数创建布尔值(而是使用文字/原始数字),因为存在与 typeof 运算符相关的潜在问题。 typeof 运算符将布尔对象报告为“object”,而不是您可能期望的原始标签(“boolean”)。此外,文字/原始值的写入速度更快。

Boolean() 属性和方法

Boolean() 对象具有以下属性:

属性(例如,Boolean.prototype;):

  • 原型
布尔对象实例属性和方法

布尔对象实例具有以下属性和方法(不包括继承的属性和方法):

实例属性(例如,var myBoolean = false; myBoolean.constructor;):

  • 构造函数

实例方法(例如,var myNumber = false; myBoolean.toString();):

  • toSource()
  • toString()
  • valueOf()
非原始 False 布尔对象转换为 True

从 Boolean() 构造函数创建的 false 布尔对象(而不是原始值)是一个对象,并且对象会转换为 true。因此,当通过 Boolean() 构造函数创建 false 布尔对象时,该值本身会转换为 true。在下面的示例中,我演示了 false 布尔对象如何始终是“true”。

示例:sample54.html

<!DOCTYPE html><html lang="en"><body><script> var falseValue = new Boolean(false); console.log(falseValue); // We have a false Boolean object, but objects are truthy. if (falseValue) { // Boolean objects, even false Boolean objects, are truthy. console.log('falseValue is truthy'); } </script></body></html>

如果需要将非布尔值转换为布尔值,只需使用 Boolean() 构造函数,而不使用 new 关键字,返回的值将是原始值而不是布尔对象。 某些事情是假的,其他一切都是真的

已经提到过,但值得再次提及,因为它与转换有关:如果值为 0、-0、null、false、NaN、undefined,或空字符串( ""),就是false。如果在布尔上下文中使用,除上述值之外的 JavaScript 中的任何值都将转换为 true(即 if (true) {};)。

示例:sample55.html

<!DOCTYPE html><html lang="en"><body><script> // All of these return a false Boolean value. console.log(Boolean(0)); console.log(Boolean(-0)); console.log(Boolean(null)); console.log(Boolean(false)); console.log(Boolean('')); console.log(Boolean(undefined)); console.log(Boolean(null)); // All of these return a true Boolean value. console.log(Boolean(1789)); console.log(Boolean('false')); // 'false' as a string is not false the Boolean value. console.log(Boolean(Math)); console.log(Boolean(Array())); </script></body></html> 结论

了解哪些 JavaScript 值被简化为 false 至关重要,这样您就知道所有其他值都被视为 true。

以上就是判断是否为布尔值的方法的详细内容,更多请关注知识资源分享宝库其它相关文章!

版权声明

本站内容来源于互联网搬运,
仅限用于小范围内传播学习,请在下载后24小时内删除,
如果有侵权内容、不妥之处,请第一时间联系我们删除。敬请谅解!
E-mail:dpw1001@163.com

分享:

扫一扫在手机阅读、分享本文

发表评论
热门文章
  • 华为 Mate 70 性能重回第一梯队 iPhone 16 最后一块遮羞布被掀

    华为 Mate 70 性能重回第一梯队 iPhone 16 最后一块遮羞布被掀
    华为 mate 70 或将首发麒麟新款处理器,并将此前有博主爆料其性能跑分将突破110万,这意味着 mate 70 性能将重新夺回第一梯队。也因此,苹果 iphone 16 唯一能有一战之力的性能,也要被 mate 70 拉近不少了。 据悉,华为 Mate 70 性能会大幅提升,并且销量相比 Mate 60 预计增长40% - 50%,且备货充足。如果 iPhone 16 发售日期与 Mate 70 重合,销量很可能被瞬间抢购。 不过,iPhone 16 还有一个阵地暂时难...
  • 酷凛 ID-COOLING 推出霜界 240/360 一体水冷散热器,239/279 元

    酷凛 ID-COOLING 推出霜界 240/360 一体水冷散热器,239/279 元
    本站 5 月 16 日消息,酷凛 id-cooling 近日推出霜界 240/360 一体式水冷散热器,采用黑色无光低调设计,分别定价 239/279 元。 本站整理霜界 240/360 散热器规格如下: 酷凛宣称这两款水冷散热器搭载“自研新 V7 水泵”,采用三相六极马达和改进的铜底方案,缩短了水流路径,相较上代水泵进一步提升解热能力。 霜界 240/360 散热器的水泵为定速 2800 RPM 设计,噪声 28db (A)。 两款一体式水冷散热器采用 27mm 厚冷排,...
  • 惠普新款战 99 笔记本 5 月 20 日开售:酷睿 Ultra / 锐龙 8040,4999 元起

    惠普新款战 99 笔记本 5 月 20 日开售:酷睿 Ultra / 锐龙 8040,4999 元起
    本站 5 月 14 日消息,继上线官网后,新款惠普战 99 商用笔记本现已上架,搭载酷睿 ultra / 锐龙 8040处理器,最高可选英伟达rtx 3000 ada 独立显卡,售价 4999 元起。 战 99 锐龙版 R7-8845HS / 16GB / 1TB:4999 元 R7-8845HS / 32GB / 1TB:5299 元 R7-8845HS / RTX 4050 / 32GB / 1TB:7299 元 R7 Pro-8845HS / RTX 2000 Ada...
  • python中def什么意思

    python中def什么意思
    python 中,def 关键字用于定义函数,这些函数是代码块,执行特定任务。函数语法为 def (参数列表)。函数可以通过其名字和圆括号调用。函数可以接受参数作为输入,并在函数体中使用参数名访问。函数可以使用 return 语句返回一个值,它将成为函数调用的结果。 Python 中 def 关键字 在 Python 中,def 关键字用于定义函数。函数是代码块,旨在执行特定任务。 语法 def 函数定义的语法如下: def (参数列表): # 函数体 示例 定义...
  • python中int函数的用法

    python中int函数的用法
    int() 函数将值转换为整数,支持多种类型(字符串、字节、浮点数),默认进制为 10。可以指定进制数范围在 2-36。int() 返回 int 类型的转换结果,丢弃小数点。例如,将字符串 "42" 转换为整数为 42,将浮点数 3.14 转换为整数为 3。 Python 中的 int() 函数 int() 函数用于将各种类型的值转换为整数。它接受任何可以解释为整数的值作为输入,包括字符串、字节、浮点数和十六进制表示。 用法 int(object, base=10) 其中...