Boolean type

Boolean is one of the most commonly used types in JavaScript. It has only two values, true and false. Other values can also be converted to booleans using the Boolean() function. Such as:

let list = 0
console.log(Boolean(list));
Copy the code

The output is false

Where, the law of converting different data into Boolean values is

The data type The value converted to true Convert to a value of false
Boolean true false
string Non-empty string “” (empty string)
Number Non-zero values (including infinity) 0, NaN
Object Any object null
Undefined N/A(non-existent) undefined

Number

The Number type is a Number, and the most common is decimal.

var message = 53
Copy the code

Octal or hexadecimal values can also be assigned. Octal is preceded by a 0 and cannot be followed by more than 8. If one of the digits is greater than or equal to 8, it is converted back to decimal

Let list = 076 // octal console.log(list);Copy the code

The output



Add 0x to hexadecimal

Let list = 0xf1 // hexadecimal console.log(list);Copy the code

The output

1. The floating point value

To define a floating point value, the value must contain a decimal point, and there must be a number after the decimal point. Such as:

Var message1 = 10.1 var message2 = 5.0 var message3 =.1 // This parameter is valid, but not recommendedCopy the code

For very large or very small numbers, scientific notation can be used, with e followed by a power

let list1 = 2126356418e-7 console.log(list1); // output 212.6356418 let list2 = 2.126e7 console.log(list2); / / output 21260000Copy the code

Floating-point arithmetic is not very precise in JS

Let list1 = 0.1; Let list2 = 0.2; let list3 = list1 + list2; console.log(list3); / / output 0.30000000000000004Copy the code

So try not to use JS in high precision scenarios

2. Value range

Due to memory limitations, JS cannot support all the values in the world. The minimum value is stored in number.min_value (non-negative, as mentioned below), which in most browsers is 5E-324; The maximum value is number. MAX_VALUE, which in most browsers is 1.7976931348623157e+308.

If you go beyond the maximum

console.log(Number.MAX_VALUE + Number.MAX_VALUE);
Copy the code

It will print infinity



if

console.log(Number.MIN_VALUE + Number.MIN_VALUE);
Copy the code

A lot of people would think that the output would be -infinity, the result



Because MIN_VALUE is not minus minimum, it’s the closest thing to zero if

console.log(-Number.MAX_VALUE - Number.MAX_VALUE);
Copy the code

It will print -infinity.



To determine whether a value is infinite, use isFinite

let message = Number.MAX_VALUE + Number.MAX_VALUE
console.log(isFinite(message));
Copy the code

Return false if infinity, true if not

3.NaN

NaN means Not a Number.

console.log(0/0); //NaN 
console.log(-0/+0); //NaN 
Copy the code
console.log(5/0); //Infinity
console.log(5/-0); //-Infinity
Copy the code

NaN has a special property that is not equal to any value, including itself

console.log(NaN == NaN); //false
Copy the code

Js provides a method to determine NaN isNaN

console.log(isNaN(NaN)); //true console.log(isNaN(10)); //false 10 is the value console.log(isNaN("10")); //false converts the value console.log(isNaN("blue")); //true cannot convert numeric values console.log(isNaN(true)); //false converts numeric valuesCopy the code

4. Numerical conversion

There are three functions in JS that convert non-numeric values to numeric values: Number(), parseInt(), parseFloat()

1. The Number () method

Number() can be used for any data type, converted based on the following rules

  • Boolean values that convert true to 1 and false to 0
  • Value, return directly
  • Null returns 0
  • String, follow the following rules
    • If the string contains characters, including numeric characters preceded by a plus or minus sign, it is converted to a decimal value. For example, Number(“1”) returns 1, Number(“123”) returns 123, and Number(“011”) returns 11 (ignoring the preceding 0).
    • If the string contains a floating-point number, such as “1.1”, the floating-point number is returned directly
    • If the string contains a valid hexadecimal, such as “0xf”, it is converted to the corresponding hexadecimal decimal value.
    • If it is an empty string, 0 is returned
    • If the string contains characters other than the above, NaN is returned.
  • Object that calls the valueOf() method and converts the returned value according to the rules above. If the result of the conversion is a NaN, the same String() method is called and converted according to the rules for converting strings.
console.log(Number("Hello world!" )); //NaN console.log(Number("")); //0 console.log(Number("0000010")); //10 console.log(Number(true)); / / 1Copy the code

2. The parseInt () method

ParseInt () prefers to convert strings, 1. If it is a string, the conversion begins at the first, or if the first is a number, plus or minus, it begins at the first and returns a value until it is not a value. If the first is not a number, return NaN directly, such as “1234Blue”, and return “1234” 2. An empty string returns NaN 3. An integer part with a decimal point, such as 22.5, returns 22 4. Can recognize different integer formats (decimal, octal, hexadecimal). It starts with 0 and is converted to octal; Starting with 0x, it will be converted to hexadecimal

console.log(parseInt("1234blue")); //1234 console.log(parseInt("")); //NaN console.log(parseInt("0xA")); / / 10. The console log (parseInt (22.5)); / / 22.5 console. The log (parseInt (" 70 ")); //70 console.log(parseInt("0xf")); / / 15Copy the code

ParseInt can also take a second argument, which is used to specify the base (base)

console.log(parseInt("f",16)); //15
console.log(parseInt("f"));    //NaN
Copy the code

3. The parseFloat () method

ParseFloat () is similar to parseInt() except that parseFloat() is resolved to the end of the string or to an invalid floating-point character, that is, until the first decimal point is valid and the second decimal point is invalid.

Another support difference is that it always ignores the 0 at the beginning of the string. Hexadecimal always returns 0, so you can only parse decimal, not specify base

console.log(parseFloat("1234blue")); //1234 console.log(parseFloat("0xA")); / / 0 console. The log (parseFloat (" 22.5 ")); / / 22.5 console. The log (parseFloat (" 22.34.6 ")); / / 22.34 console. The log (parseFloat (" 0908.5 ")); / / 908.5 console. The log (parseFloat (" 3.125 e7 ")); / / 31250000Copy the code