一个小数为什么用isNaN和typeof判断不相符?

var arr = [119.657958,29.108071];
function c(){
  var x = ((Math.random()-0.5)/500).toFixed(6);
  console.log(x);
  return x;
}
let y = c();
console.log(isNaN(y));
console.log(typeof y);
console.log(y+1);  

结果为

clipboard.png

为什么y既是数字又是字符串呢

阅读 3.4k
5 个回答

使用isNaN会使用Number进行隐式转换

//toFixed(),返回的就是number Object 的字符串显示,判断其类型使用Object.prototype.toString.call(12.12331.toFixed(3))  
//"[object String]" 其结果就是字符串
//isNaN判断为true
//y+1,将number 1 转换成string,最后就是字符串相加

toFixed() 的返回值是string类型的

问题大家都答得差不多啦, 我就是想问楼主这个isNaN()是怎么做到返回true的?

新手上路,请多包涵

toFixed()返回值是string类型的,输入的值是number类型的

推荐问题