JS 基础1: 理解number 数据类型 和隐式类型转换
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS 基础1: 理解number 数据类型 和隐式类型转换相关的知识,希望对你有一定的参考价值。
1, javascript only has one type of numberic data, named "number". You can see this reflected in the behavior of the typeof operator, which classifies intergers and floating-point numbers alike simply as numbers:
typeof 10; // "number" typeof 10.3; // "number" typeof -1.5; // "number"
2, In fact, all numbers are double-precision floating-point numbers, known as "doubles", which is 64-bit encolding of numbers, keep in mind that doubles can represent intergers perfectly with up to 53 bits of precison. All of the integers from -253 to 253 are valid doubles.
3, Note that floating-point numbers are inaccurate when in arithmetic, for example, 0.1+0.2 => 0.30000000000004;
It is better to tranform the floating-point number to intergers for calculation. 10 + 20 => 30;
-----------------------------------------------------------------------------------------------------------------------------------
4, coercisons can also hide errors(隐式类型转换可能隐藏错误)。
an undefined variable will convert to the NaN in an arithmetic calculation, which causes the calculation ot continue with often confusing and unprdictable results.
以上是关于JS 基础1: 理解number 数据类型 和隐式类型转换的主要内容,如果未能解决你的问题,请参考以下文章