10.toFixed(10)【前端每日一题-16】

570 阅读1分钟

10.toFixed(10)输出什么?为什么?

考点:

  • js浮点数

答案:

10.toFixed(10)会报错。

10.toFixed(10) //Uncaught SyntaxError: Invalid or unexpected token

为什么?

之所以会报错时因为这里的.发生了歧义,既可以理解成浮点数的小数点,又可以理解为对方法的调用。因为数字后面紧跟着一个.,解释器就默认为浮点数,所以会报错。

解决方法

console.log(10 .toFixed(10));
console.log((10).toFixed(10));
console.log(10..toFixed(10));
console.log(10.0.toFixed(10));