First, JS data type
Raw data type: Null number String Undefined Boolean symbol (denotes unique value) about Symbol https://www.jianshu.com/p/174f88d189c6
Reference type Object
Second, type conversion
1.Number function
- String: Converts to a number if it can be resolved to a number, otherwise Nan, an empty string is converted to 0
- Boolean value: True to convert to 1,false to 0
- null:0
- Undefined:nan
The valueof method is called first, and if the method returns data of the original type, the number conversion is called directly;
If the ValueOf method returns a value of the reference type and then calls the object's ToString method, if the ToString method returns the data of the original type, then the number is converted;
If the ToString method returns a reference type, an error occurs.
2.String function
- Value: A string converted to a response
- Boolean: True converts to "true", false to "false"
- Null: Converted to "null"
- Undefined: "Undefined"
The ToString method is called first, and if the method returns data of the original type, the string conversion is called directly;
If the ToString method returns a value of a reference type, then the ValueOf method is called, and if the ValueOf method returns the data of the original type, then the string is converted;
If the ValueOf method returns a reference type, an error occurs.
3.Boolean function
Undefined null-0 +0 "(empty string) NaN is converted to false, the rest is true
Interesting topics:
JS type conversion