首页 > 代码库 > JS 基础1: 理解number 数据类型 和隐式类型转换

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 数据类型 和隐式类型转换