zoukankan      html  css  js  c++  java
  • 内置函数isNaN()

    NaN(not a number)的产生:算术运算返回一个未定义的或无法表示的值

    1.NaN并不一定用于表示某些值超出表示范围的情况。将某些不能强制转换为数值的非数值转换为数值的时候,也会得到NaN。例如,0 除以0会返回NaN, 但是其他数除以0则不会返回NaN

    2.NaN不能通过相等操作符(== 和 ===)来判断 ,因为 NaN == NaN 和 NaN === NaN 都会返回 false

    3.怪异行为:如果isNaN函数的参数不是Number类型, isNaN函数会首先尝试将这个参数转换为数值,然后才会对转换后的结果是否是NaN进行判断。因此,对于能被强制转换为有效的非NaN数值来说(空字符串和布尔值分别会被强制转换为数值0和1),返回false

    例子:

        console.log(isNaN(123));  // false

        console.log(isNaN('string'==='string')); // false

        console.log(isNaN(undefined===undefined)); // false

        isNaN(12/0);      //false

        isNaN(true);      // false

        isNaN(null);      // false

      isNaN(37);        // false

        isNaN("37");      //  false: 可以被转换成数值37

        isNaN("37.37");   // false: 可以被转换成数值37.37

        isNaN("");        // false: 空字符串被转换成0

        isNaN(" ");       // false: 包含空格的字符串被转换成0

        isNaN(new Date());          // false

        console.log(isNaN('dc'));  // true

        console.log(isNaN('dc12345')); // true

        console.log(isNaN('undefined'));   // true 

        isNaN(NaN);       // true

        isNaN(undefined); // true

        isNaN({});        // true

        isNaN(0/0);       //true

        isNaN("37,5");    // true

        isNaN('123ABC');  // true:  parseInt("123ABC")的结果是 123, 但是Number("123ABC")结果是 NaN

        isNaN(new Date().toString());     // true

        isNaN("blabla")   // true: "blabla"不能转换成数值

  • 相关阅读:
    教你用纯Java实现一个网页版的Xshell(附源码)
    Learn Terraform--Getting Started--Installing Terraform
    Install Terraform on Windows, Linux and Mac OS
    Installing kubectl
    Creating a Cron Job in K8S
    Patch multi versions of windows via Power shell
    Bash to check SSL cert expired
    K8S dashboard 创建只读账户
    K8S Link
    Kubernetes
  • 原文地址:https://www.cnblogs.com/prospective-zkq/p/9698540.html
Copyright © 2011-2022 走看看