First, the data type
5 Basic data types: Null/undefined/string/boolean/number
1 Complex data types: Object
Second, data type detection
Portal "Four kinds of JS data types and their limitations"
1, typeof
2, Instanceof/constructor
3, Object.prototype.toString.call (value)
4, Object.prototype.toString
Iii. Conversion of data types
JS internally provides an automatic conversion mechanism for different data types, which is automatically converted to the expected type when it is expected to be a type rather than a type, which is what we often call implicit conversions.
1. Forced type conversion
Before you know the rules for implicit conversions, take a look at coercion type conversions, which are forced by the Boolean ()/string ()/number () to convert data of each type to Boolean, string, and numeric data.
Boolean () function
The Boolean () function returns True when the value to be converted is a string of at least one character, not a 0 number, or an object. If the value is an empty string, the number 0, undefined, or null, it returns FALSE.
var B1 = Boolean ("");//false-empty string var B2 = boolean ("Hello");//true-Non-empty string var B1 = Boolean;//true-non 0-digit var B1 = Bool EAN (null);//false-nullvar B1 = Boolean (0),//false-0 var B1 = Boolean (New object ());//true-Object
Number () function
The coercion type conversion of the number () function is similar to the parseint () and parsefloat () methods, except that it transforms the entire value, not the partial value.
The parseint () and parsefloat () methods only convert strings before the first invalid character, so "1.2.3" is converted to "1" and "1.2" respectively.
Coercion type conversion with number (), "1.2.3" returns NaN because the entire string value cannot be converted to numbers. If the string value can be fully converted, number () will determine whether to call the parseint () method or the Parsefloat () method.
String () function
The last method of forcing a type conversion string () is the simplest because it converts any value to a string.
To perform this coercion type conversion, you only need to call the ToString () method that passes in the value as a parameter, that is, convert 12 to "12", convert True to "true", convert false to "false", and so on.
The only difference between casting to a string and calling the ToString () method is that coercion of type conversions on null and undefined values can generate strings without throwing an error:
var S1 = String (null),//"null" var onull = Null;var s2 = onull.tostring ();//Throws an error
2. Automatic type conversion
Finish the coercion type conversion, then take a look at the automatic type conversion, in fact, the automatic type conversion is based on the coercion type conversion, when the expected location should be a type (Boolean, numeric, string) data, it will invoke the corresponding coercion type conversion function, this is automatic.
* When JavaScript encounters an expected Boolean value (such as if
the conditional part of the statement), the non-Boolean parameter is automatically converted to a Boolean value. Functions are automatically called inside the system Boolean
.
Therefore, in addition to the following six values, the others are automatically converted true
.
undefined
null
-0
0
Or+0
NaN
‘‘
(empty string)
* When JavaScript encounters the expected string, the non-string data is automatically converted to a string. Functions are automatically called inside the system String
.
The automatic conversion of strings occurs mainly during addition operations. When one value is a string and the other value is non-string, the latter is converted to a string.
* When JavaScript encounters a value that is expected, the parameter value is automatically converted to a number. Functions are automatically called inside the system Number
.
In addition to the addition operator, it is possible to convert an operator to a string, and other operators will automatically turn the operator into a numeric value.
The unary operator also turns the operator into a numeric value.
+ ' abc '//nan-' abc '//nan+true//1-false//0
Smoothing out JavaScript data type conversion rules