isNaN()

返回值:   如果给定值为 NaN则返回值为true;否则为false

NaN值的产生:

当算术运算返回一个未定义的或无法表示的值时,NaN就产生了

NaN并不一定用于表示某些值超出表示范围的情况

将某些不能强制转换为数值的非数值转换为数值的时候,也会得到NaN

如果isNaN函数的参数不是Number类型,isNaN函数会首先尝试将这个参数转为数值,

然后才会对转换后的结果进行isNaN判断.

例子🌰:

console.log(isNaN(NaN));         //true
console.log(isNaN(undefined));   //true
console.log(isNaN({}));          //true
console.log(isNaN(0/0));         //true
console.log(isNaN('88,99'));     //true
console.log(isNaN());            //true
console.log(isNaN('123abc'));    //true
console.log(isNaN('hello'))      //true
console.log(isNaN(new Date().toString())); //true

console.log(isNaN([]));          //false
console.log(isNaN(false));       //false
console.log(isNaN(true));        //false
console.log(isNaN(null));        //false
console.log(isNaN(''));          //false
console.log(isNaN(' '));         //false
console.log(isNaN('8'));         //false
console.log(isNaN('8.9'));       //false

手动封装一个isNaN

function isNaN1(num) {
  var res = Number(num) + '';
  if(res == 'NaN'){   // 注意这里NaN不等于任何数,包括NaN本身
    return false
  } else {
    return true
  }
}
console.log(isNaN('123'));

有用的特殊行为

有许多方式来看待isNaN():如果isNaN(x)返回false,那么x在任何算数表达式中都不会使表达式等于NaN;如果返回true,x会使所有算数表达式返回NaN。这就意味着,在JavaScript中,isNaN(x)==true等价于x-0=NaN(在JavaScript中 x-0 == NaN 总是返回false,所以你不用去测试它)。实际上, isNaN(x)isNaN(x - 0),isNaN(Number(x))Number.isNaN(x - 0),和Number.isNaN(Number(x)) 的返回值都是一样的 并且在JavaScript中isNaN(x)是这些表达式中最短的表达。

举个例子,可以利用这个特殊行为来检测函数的参数是可运算的(可以像number一样进行加减乘除等运算)。如果不可运算,则可赋予这个参数一个默认的值或其他合适的内容。这样,就可以得到一个隐式转换参数值的函数,而这得益于Javascript的全功能性。

例子

function increment(x) {
  if (isNaN(x)) x = 0;
  return x + 1;
};

// The same effect with Number.isNaN():
function increment(x) {
  if (Number.isNaN(Number(x))) x = 0;
  return x + 1;
};

// In the following cases for the function's argument x,
// isNaN(x) is always false, although x is indeed not a
// number, but can be used as such in arithmetical
// expressions
increment("");            // 1: "" is converted to 0
increment(new String());  // 1: String object representing an empty string is converted to 0
increment([]);            // 1: [] is converted to 0
increment(new Array());   // 1: Array object representing an empty array is converted to 0
increment("0");           // 1: "0" is converted to 0
increment("1");           // 2: "1" is converted to 1
increment("0.1");         // 1.1: "0.1" is converted to 0.1
increment("Infinity");    // Infinity: "Infinity" is converted to Infinity
increment(null);          // 1: null is converted to 0
increment(false);         // 1: false is converted to 0
increment(true);          // 2: true is converted to 1
increment(new Date());    // returns current date/time in milliseconds plus 1

// In the following cases for the function's argument x,
// isNaN(x) is always false and x is indeed a number
increment(-1);            // 0
increment(-0.1);          // 0.9
increment(0);             // 1
increment(1);             // 2
increment(2);             // 3
// ... and so on ...
increment(Infinity);      // Infinity

// In the following cases for the function's argument x,
// isNaN(x) is always true and x is really not a number,
// thus the function replaces it by 0 and returns 1
increment(String);            // 1
increment(Array);             // 1
increment("blabla");          // 1
increment("-blabla");         // 1
increment(0/0);               // 1
increment("0/0");             // 1
increment(Infinity/Infinity); // 1
increment(NaN);               // 1
increment(undefined);         // 1
increment();                  // 1

// isNaN(x) is always the same as isNaN(Number(x)),
// but the presence of x is mandatory here!
isNaN(x) == isNaN(Number(x))  // true for every value of x, including x == undefined,
                              // because isNaN(undefined) == true and Number(undefined) returns NaN,
                              // but ...
isNaN() == isNaN(Number())    // false, because isNaN() == true and Number() == 0

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值