Write on the front: There are many data types in JavaScript. In practice, whether intentionally or unintentionally, we can always encounter different data type values for calculation, or I want to get a number from the user input, while the user input a string, this time we need to use today's introduction. What I've talked about is data type conversion. There are two main types of data type conversion in JS: display type conversion and implicit type conversion.
Display type conversion
1,Number()
Accept a parameter and convert it into a number.
If the parameter contains characters other than numbers and decimal points, the conversion fails and NaN is returned. The following is the result of converting all special values into numbers:
1 Number(null);//0 2 Number(undefined);//NaN 3 Number(true);//1 4 Number(false);//0 5 Number("");//0
2,parseInt()
Accept two parameters, the first parameter will be converted to decimal integer, the second parameter is a number, specify the first parameter's decimal, it is optional, default is 10.
Conversion rules: First, determine the data type of the first parameter, if it is not a string or a number type, then the conversion fails and returns NaN directly; if it is a number, it returns as it is; if it is a string, it starts from the first non-blank character and converts sequentially until the first non-blank character is encountered. A number type returns to the previous section. If the positive and negative sign is the first non-empty character, then the positive and negative sign can be recognized. But decimal points cannot be recognized.
1 parseInt("123");//123 2 parseInt("-123");//-123 3 parseInt(123.123);//123 4 parseInt(" 123");//123 5 parseInt("123abc");//123 6 parseInt("abc123");//NaN 7 parseInt(true);//NaN 8 parseInt(undefined);//NaN 9 parseInt(unll);//NaN 10 parseInt("");//NaN
11 parseInt(110,2);//6
12 parseInt(110,16);//272
3,parseFloat()
Accept a parameter and convert it to decimal. If the positive and negative sign is the first non-empty character, then the positive and negative sign can be recognized. The only difference from parseInt is that it recognizes the first decimal point.
1 parseFloat("123.123");//123.123 2 parseFloat("123.1.2.3");//123.1 3 parseFloat("123.123abc");//123.123
4,toString()
Call the toString() method with a value that needs to be converted into a string in the format: test.toString(), note that the number cannot be called directly, and the number needs to be assigned to the variable call. The return values of the toString() method are all string types.
When converting a number, a number type can be accepted as a parameter, which specifies the base of the converted result. Otherwise, the reference is invalid.
1 var num = 10; 2 var a = num.toString();//"10" 3 var b = num.toString(2);//"1010" 4 var c = num.toString(8);//"12" 5 var d = num.toString(16);//"a"
5,String()
Accept a parameter and convert it into a string. Anything passed in can get a return value of the string type.
1 String(123);//"123" 2 String(123.123);//"123.123" 3 String(undefined);//"undefined" 4 String(null);//"null" 5 String(true);//"true"
6,Boolean()
Everything else is converted to true except undefined, null, 0, NaN, "(empty string), false to false.
1 Boolean(undefined);//false 2 Boolean(null);//false 3 Boolean(0);//false 4 Boolean(NaN);//false 5 Boolean("");//false 6 Boolean(false);//false
Two Implicit Type Conversion
1,isNaN()
Accept a parameter to determine whether the parameter is a number, return false, or return true.
Implicit type conversion is triggered by calling Number() to convert parameters to digital types.
1 isNaN(123);//false 2 isNaN("123");//false 3 isNaN("");//false 4 isNaN("abc");true
2, +/-(self-increasing and self-decreasing) and +/-(positive and negative)
Implicit type conversion is triggered by calling Number() to convert operands to digital types.
1 var num = "123"; 2 num ++;//123 3 console.log(num);//124
3, + (Number and String Add Time)
Implicit type conversion is triggered by calling String() to convert operands to string types.
1 var num = 12; 2 console.log("I'm " + num + "years old.");//"I'm 12 years old" 3 console.log(num + "12");//"1212"
4,-,*,/,%
Implicit type conversion is triggered by calling Number() to convert operands to digital types.
1 var num = 12; 2 console.log(num - 1);//11 3 console.log(num * 1);//12 4 console.log(num / 1);//12 5 console.log(num % 1);//0
5,&&,||,!
Implicit type conversion is triggered, and Boolean() is called to convert each operand to a Boolean type.
1 123 && "abc";//"abc" 2 0 && "abc";//0 3 123 || "abc";//123 4 0 || undefined;//undefined 5 !0;//true 6 !1;//false
6,>,<,<=,>=,=,=,!= (when comparing numbers with strings)
Implicit type conversion is triggered by calling Number() to convert operands to digital types.
1 123 > "122";//true 2 123 < "124";//true 3 123 >= "123";//true 4 123 <= "123";//true 5 123 == "123";//true 6 123 != "123";//false
Finally, there are two special things: undefined = null returns true; NaN = NaN returns false. In addition, string priority is not used in addition (+) operation, while number priority is used in other mathematical calculations.