This article describes the basic data types and conversion rules of Javascript. It has good reference value. If you need it, you can check the data type.
Five basic data types: Null/Undefined/String/Boolean/Number
One complex data type: Object
Ii. Data Type Detection
1. typeof
2. instanceof/constructor
3. Object. prototype. toString. call (value)
4. Object. prototype. toString
Iii. Data Type Conversion
JS provides an automatic conversion mechanism for different data types. It is automatically converted to the expected type when it is expected to be of a certain type instead of a certain type. This is what we call implicit conversion.
1. Forced type conversion
Before learning about implicit conversion rules, let's take a look at forced type conversion. Forced type conversion mainly uses Boolean ()/String ()/Number () converts various types of data into boolean, string, and numeric data.
Boolean () function
If the value to be converted is a string of at least one character, a non-zero number, or an object, the Boolean () function returns true. If the value is a null String, 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 (50 ); // true-non-zero numeric var b1 = Boolean (null); // false-nullvar b1 = Boolean (0 ); // false-zero var b1 = Boolean (new object (); // true-object
Number () function
The forced type conversion of the Number () function is similar to that of the parseInt () and parseFloat () methods, except that it converts the entire value instead of the partial value.
The parseInt () and parseFloat () methods only convert strings before the first invalid character. Therefore, "1.2.3" is converted to "1" and "1.2" respectively ".
Use Number () for forced type conversion. "1.2.3" will return NaN because the entire string value cannot be converted to a Number. If the string value can be fully converted, Number () determines whether to call the parseInt () method or parseFloat () method.
String () function
The last forced type conversion method String () is the simplest, because it can convert any value into a String.
To perform this forced type conversion, you only need to call the toString () method of the value passed as the parameter, that is, convert 12 to "12", and convert true to "true ", convert false to "false", and so on.
The only difference between forced conversion to a string and calling the toString () method is that forced type conversion to null and undefined values can generate a string without causing an error:
Var s1 = String (null); // "null" var oNull = null; var s2 = oNull. toString (); // an error is thrown.
2. Automatic type conversion
After the mandatory type conversion is completed, let's take a look at the automatic type conversion. In fact, the automatic type conversion is based on the forced type conversion. When a specified position is expected to be a certain type (Boolean, numeric, string) will call the corresponding forced type conversion function, which is automatically performed.
* When JavaScript encounters a place where it is expected to be a Boolean value (such as the condition section of the if statement), it will automatically convert a non-Boolean value to a Boolean value. The system automatically calls the Boolean function.
Therefore, all values except the following six values are automatically converted to true.
Undefined
Null
-0
0 or + 0
NaN
''(Null String)
* When JavaScript encounters a string that is expected, it automatically converts non-string data to a string. The system automatically calls the String function.
The automatic conversion of strings mainly occurs in addition operations. If one value is a string and the other value is not a string, the latter is converted to a string.
* When JavaScript encounters a value that is expected, the parameter value is automatically converted to a value. The system automatically calls the Number function.
Apart from addition operators, you may convert the operator into a string. Other operators automatically convert the operator into a value.
The unary operator also converts an operator into a value.
+'abc' // NaN-'abc' // NaN+true // 1-false // 0
The above is all the content of this article. I hope this article will help you in your study or work, and I also hope to support PHP!
For more details about the conversion rules of Javascript data types, please follow the PHP Chinese network!