zoukankan      html  css  js  c++  java
  • typeof检测几种较为特殊的值与Boolean强制转换哪几种值为false Amy

    typeof是操作符,不是函数,可以用它来检测给定变量的数据类型,共有6个取值:"undefined"/"number"/"string"/"object"/"function"/"boolean"

    下面是几种较为特殊的值

    alert(typeof NaN);//number
    alert(typeof Number.MAX_VALUE);//number
    alert(typeof Number.POSITIVE_INFINITY);//number
    var a=function(){
    };
    alert(typeof a);//function
    alert(typeof function(){});//function
    alert(typeof {});//object
    alert(typeof []);//object
    alert(typeof null);//object
    //
    alert(typeof b);//undefined
    var c;
    alert(typeof c);//undefined
    //未初始化未定义typeof返回的均为undefined,但是它们还是有区别的,如果alert一个从未定义的变量alert(b)//出错
    //如果alert一个未初始化的变量alert(c)//undefined

     除了下面5个特殊值,其他任何类型的数据转换为逻辑值时都是true,NaN,null,undefined,0,""

    alert(Boolean(NaN));//false
    alert(Boolean(undefined));//false
    alert(Boolean(""));//false
    alert(Boolean(0));//false
    alert(Boolean(null));//false
  • 相关阅读:
    P1182 数列分段 Section II 题解
    P3853 路标设置题解
    二分模板
    P2678 跳石头题解
    P2440 木材加工题解
    P1024 一元三次方程求解题解
    快速下载vscode的方法
    P1824 进击的奶牛题解
    P1873 砍树题解
    用户登录之asp.net cookie的写入、读取与操作
  • 原文地址:https://www.cnblogs.com/amy2011/p/3131636.html
Copyright © 2011-2022 走看看