JavaScript Base Type Value Number Type

  • 2021-07-24 09:16:39
  • OfStack

A general introduction

In JavaScript, IEEE754 format is used to represent numbers, so integers and floating-point numbers are not distinguished, and they are stored in the form of 64-bit floating-point numbers. That is to say, inside JavaScript, there is no decimal at all. However, some operations must be completed by integers, so JavaScript sometimes converts 64-bit floating-point numbers into 32-bit integers before performing operations.

Integer

JavaScript provides four representations of integers:

1, binary: A number with the prefix 0b, and a number other than 0, 1 will report an error

2, octal: A value prefixed with 0o, or followed by a number (0-7). If the value range described above is exceeded, the first digit 0 is ignored and regarded as a decimal number

Note: Octal literals are not valid in strict mode, causing the JavaScript engine that supports this mode to throw an error

3. 106-ary: Prefixed with 0x, followed by any 106-ary digits (0 ~ 9 and A ~ F), letters can be uppercase or lowercase, and errors will be reported if they exceed the range

4, decimal


var num2 = 0b11;
 console.log(num2); //3
 var num2 = 0b12;
 console.log(num2); // Report an error 
 var num8 = 0o76;
 console.log(num8); //02
 var num8 = 0o78;
 console.log(num8); // Report an error 
 var num16 = 0x2a;
 console.log(num16); //42
 var num16 = 0x2h;
 console.log(num16) // Report an error 

Floating point number

The so-called floating point number means that the value must contain 1 decimal point, and there must be at least 1 digit after the decimal point. Unlike integers, floating-point numbers can only be expressed in decimal

Floating-point numbers are far less accurate than integers, so you should be careful when designing floating-point numbers

For example:


 console.log(0.1 + 0.2 == 0.3); //false 0.30000000000000004
 console.log(0.6/0.2); //2.9999999999999996

Scientific notation

For those minimax values, they can be represented by floating-point values represented by e notation (that is, scientific notation). The value represented by the e notation is equal to the value before e multiplied by the exponential power of 10

In the following two cases, JavaScript will automatically convert the numerical value to scientific notation, and in other cases, it will be expressed literally.

1. The number before the decimal point is more than 21 digits


 console.log(1234567890123456789012);// 1.2345678901234568e+21
 console.log(123456789012365648787); //123456789012365660000   

2. 0 after the decimal point is more than 5 digits


 console.log(0.0000006);//6e-7
 console.log(0.000006); //0.000006

Numerical range

Because of memory constraints, ECMAScript can't hold all the values in the world, so it has a maximum value and a minimum value

The minimum value is stored in Number.MIN_VALUE, which is 5e-324

The maximum value is saved in Number.MAX_VALUE, which is 1.7976931348623157e+308


 console.log(Number.MIN_VALUE) //5e-324
 console.log(Number.MAX_VALUE); //1.7976931348623157e+308

If the number exceeds the maximum, javascript returns Infinity, which is called forward overflow (overflow); If it is equal to or exceeds the minimum negative value of-1023 (that is, very close to 0), javascript directly converts this number to 0, which is called negative overflow (underflow)

If you want to determine whether a value is finite, you can use the isFinite () function. This function returns true when the parameter is between the minimum and maximum values


 var result = Number.MAX_VALUE + Number.MAX_VALUE;
 console.log(isFinite(result)); //false

Special value

1, +0 and-0

These two zeros are equivalent in the case of large logarithms


 -0 === +0; //true
 0 === -0; //true
 0 === +0; //true

But when it is used as denominator, it is not 1

1/-0 == 1/+0; //false

2. infinity

Infinity stands for "infinity" and is used to represent two scenarios. One is that a positive value is too large, or a negative value is too small to represent; The other non-0 value is divided by 0 to obtain Infinity.


 Math.pow(2,Math.pow(2,100));//Infinity
 1/0;//Infinity

Infinity participates in operations that result only in itself, 0 or NaN


* Infinity;//Infinity
- Infinity;//-Infinity
+ Infinity;//Infinity
/ Infinity;//0
 Infinity / 2;//Infinity
 Infinity * Infinity;//Infinity
 Infinity - Infinity;//NaN
 Infinity + Infinity;//Infinity
 Infinity / Infinity;//NaN

3. NaN

This numeric value indicates that an operand that was intended to return a numeric value did not return a numeric value

ES 116EN is not equal to any value, including itself, and any operation involving ES 117EN returns ES 118EN


 5 - 'x'; //NaN
 Math.acos(2); //NaN
 0 / 0; //NaN
 NaN == NaN;//false
 NaN == Infinity;//false 

NaN is not an independent data type, but a special value, and its data type still belongs to Number

 typeof NaN; //number

The isNaN method can be used to determine whether a value is NaN, but isNaN is only valid for numeric values, and if other values are passed in, they will be converted to numeric values first. For example, when a string is passed in, the string will be converted to NaN first, so true will be returned at last, which should be paid special attention to. That is, isNaN is the value of true, which may not be NaN, but a string.


 console.log(0.1 + 0.2 == 0.3); //false 0.30000000000000004
 console.log(0.6/0.2); //2.9999999999999996
0

A more reliable way to judge NaN is to make use of the characteristic that NaN is the only one in javascript that is not equal to its own value


 console.log(0.1 + 0.2 == 0.3); //false 0.30000000000000004
 console.log(0.6/0.2); //2.9999999999999996
1

Number system conversion

There are three functions that convert non-numeric values to numeric values: Number (), parseInt (), and parseFloat (). Where Number () converts any type of value to a numeric value, parseInt () and parseFloat () apply only to string-to-numeric conversions

Number()

Conversion rules:

1. If it is an Boolean value, true and false will be converted to 1 and 0, respectively

2. If it is an null value, return 0

3. If it is undefined, return NaN

4. If it is a string, follow the following rules:

(1) If the string contains only decimal or 106-decimal digits, it is converted to decimal digits

Note: Number () does not recognize strings of octal digits and is treated as decimal digits

The string '1.2.' does not report an error, but the number 1.2. does

(2) If the string is an empty string or a space string, turn to 0

(3) In other cases, the string is converted to NaN


 console.log(0.1 + 0.2 == 0.3); //false 0.30000000000000004
 console.log(0.6/0.2); //2.9999999999999996
2

parseInt()

parseInt () is designed to convert strings to integers. When converting a string, the space before the string is ignored until the first non-space character is found. If the first character is not a numeric character or a minus sign, parseInt () returns NaN. If so, continue parsing until parsing is complete or non-numeric characters are encountered


 console.log(parseInt(' 123.8px'));//123
 console.log(parseInt(' 123.8 '));//123
 console.log(parseInt(' -123.8px'));//-123
 console.log(parseInt('a123.8px'));//NaN
 console.log(parseInt('0 123.8px'));//0

Note: In ECMAScript5, parseInt () no longer has the ability to parse octal. For example, "070" in octal system will ignore the preceding "0" and get 70 in decimal system

To eliminate the above confusion that can arise when using the parseInt () function, you can provide this function with a second parameter: the cardinality (how many digits) to use when converting


 console.log(0.1 + 0.2 == 0.3); //false 0.30000000000000004
 console.log(0.6/0.2); //2.9999999999999996
4

parseFloat()

parseFloat () is specifically used for string conversion floating point numbers. Similarly, the space before the string is ignored when parsing until the first non-space character is found, and then 1 is parsed straight to the end of the string or an invalid floating-point numeric character


 console.log(0.1 + 0.2 == 0.3); //false 0.30000000000000004
 console.log(0.6/0.2); //2.9999999999999996
5

Note: parseFloat () resolves only decimal, so strings in 106 will always be converted to 0. Therefore, there is no second parameter to specify the cardinality

 parseFloat("0xA") //0

Note: The result of Number ('') is 0, and the result of parseInt ('') and parseFloat ('') is NaN


Related articles: