zoukankan      html  css  js  c++  java
  • 聊一聊JS中布尔值为false哪点事儿

    在JavaScript中布尔值为false总共有六种情况:NaN , 0 , false , “”(双引号)或 ‘’(单引号) ,null , undefined下面分别详细介绍:

    1. NaN (非数值,不能计算结果时)
    //转化为布尔值
    alert(Boolean(NaN)); //false
    
    //对应的类型(typeof值)
    alert(typeof(NaN)); //number
    1. 0 (数字0
      注意点:字符串 “0” 的布尔值为 true
    //转换为布尔值
    alert(Boolean(0)); //false
    
    //对应的类型(typeof值)
    alert(typeof(0)); //number
    1. false(布尔类型的false)
      注意点:字符串类型的 “false" 布尔值为 true
    //转换为布尔值
    alert(Boolean(false)); //false
    
    //对应的类型(typeof值)
    alert(typeof(false)); //boolean
    1. 引号 (“” (双引号) 或 ‘’(单引号))
      注意点:空字符串,引号中间有空格时布尔值为 ture
    //转换为布尔值
    alert(Boolean(""));//false
    alert(Boolean(''));//false
    
    //对应的类型(typeof值)
    alert(typeof("")); //string
    alert(typeof('')); //string
    1. null (空值)
    //转换为布尔值
    alert(Boolean(null)); //false
    
    //对应的类型(typeof值)
    alert(typeof(null)); //object

    6 . undefined (没有定义)

    //转换为布尔值
    alert(Boolean(undefined)); //false
    
    //对应的类型(typeof值)
    alert(typeof(undefined); //undefined

    注意点:处理上述概括的 6 种值转化为布尔值为 false 外,其他转化为布尔值的情况都为 ture ,空数组,空对象,负值转化为布尔值也为true

    看完以上介绍,你是不是记住了什么情况布尔值为true,什么情况布尔值为false了。总之把布尔值为false那六种值,以及比较特殊的情况记下,就大功告成了。
    如果想深入了解怎么用typeof运算符判断变量是什么类型的请点击我

  • 相关阅读:
    ubuntu系统里常用的几个命令
    Vue中常用知识点demo
    Vue基础知识学习(后端)
    ubuntu内lnmp相关操作命令
    linux下安装lnmp集成环境
    js时间戳与日期格式之间相互转换
    yii2中 选择布局的方式,可以设置不使用布局
    yii2中通过migration创建数据表
    php实现支付宝在线支付和扫码支付demo
    linux下添加用户并将文件夹授权给某一个用户
  • 原文地址:https://www.cnblogs.com/oito/p/12149517.html
Copyright © 2011-2022 走看看