This is the fourth day of my participation in the August Text Challenge.More challenges in August
The data type
ECMAScript has six simple data types: Number, String, Boolean, Null, and Undefined Symbol. Symbol is newly added to ES6.
The Number type
The Number type uses the IEEE754 format to represent integers and floating point values. Since floating point values are stored in twice as much memory as integer values, ECMAScirpt always tries to convert values to integers for processing.
1. The floating point value
let floatNum1 = 0.8;
let floatNum2 = 1.8;
let floatNum3 = 8.;// Valid, but not recommended
Copy the code
Is it true that 0.1+0.2==0.3 in JS? The answer is no. Because IEEE754 values are used, there is a loss of precision in floating-point calculations, so never test a particular floating-point value, convert it to an integer as much as possible.
2. Value range
If the result of a computed value exceeds the range that JS can represent, the value is automatically converted to a special Infinity value. Any unexpressible negative value is -infinity, and any unexpressible positive value is Infinity. To determine if a value isFinite, use the function isFinite().
3.NaN
A special value NaN, which means “Not a Number,” is used to indicate that an operation intended to return a Number failed. Such as:
console.log(0/0);// NaN is printed here
Copy the code
NaN has several unique features. First of all, NaN is not equal to itself. Second, any operation that involves a NaN returns a NaN. Therefore, there is a function isNaN(). This function takes an argument and tries to convert it to a value, returning false if it can be converted to a value, or true if it cannot.
4. Numerical conversion
There are three functions that convert non-numeric values to numeric values: Number(),parseInt(), and parseFloat(). Number() is a transformation function that can be used for any data type. ParseInt () and parseFloat() are mainly used to convert strings to numbers.
For the Number() function, there are the following transformation rules:
- Boolean type: true returns 1, false returns 0;
- Number type: returns directly;
- Null: returns 0;
- Undefined: returns NaN;
- String type: an empty String returns 0; Numeric strings return decimal values; In other cases NaN is returned
- Object: call the valueof() method and return as above, or in the case of NaN, call the toString() method and convert according toString rules.
The parseInt() function is more concerned with whether the string contains a numeric pattern. If the first character is a numeric character, plus or minus, each character in turn is tested until the end of the string, or a non-numeric character is encountered. If not, return NaN immediately. And it can take a second argument that specifies the base (base), for example:
console.log(parseInt("2021juejin")); / / 2021
console.log(parseInt("")); //NaN
console.log(parseInt("0XA")); //10 is a hexadecimal integer
console.log(parseInt("20.21 juejin")); / / 20
console.log(parseInt("2021")); / / 2021
console.log(parseInt("AB".16)); / / 171
console.log(parseInt("AB")); //NaN
console.log(parseInt("10".2)); / / 2
console.log(parseInt("10".8)); / / 8
console.log(parseInt("10".10)); / / 10
console.log(parseInt("10".16)); / / 16
Copy the code
The parseFloat() function always ignores 0 at the beginning of the string and parses until the end of the string or an invalid floating-point character. And the parseFloat() function takes only one argument, parsed in decimal. Such as:
console.log(parseFloat("2021juejin")); / / 2021
console.log(parseFloat("")); //NaN
console.log(parseFloat("0XA")); / / 0
console.log(parseFloat("20.21. Juejin"));/ / 20.21
console.log(parseFloat("0.2.1")); / / 0.2
console.log(parseFloat("AB")); //NaN
Copy the code