JS type conversions are divided into display type conversions and implicit type conversions
One: Implicit type conversions
1. Implicit type conversions present in the operator
The "+" operator:
var a=123, b= "123"var c=a+B; var d=b+a;
After the "+" operation, C and D are 123123,123123 respectively. When the operator "+" is a numeric type and one is a string type, the JS engine specifies that the string join operation is not arithmetic plus.
With the operator "+", it is convenient to convert the number type X to string by x+ "" equivalent to String (x).
"-" Operator:
var a=123,b= "at"; var c=a-b;
After the "-" operation, c is 100. The operator "-" can be negative for unary operators or as a two-dollar operator "minus". The operator "-" will convert the string implicitly to a number and then perform the operation.
With the operator "-", it is convenient to convert a string of type X to number by X "" equivalent to Number (x).
The "*" Operator:
var a=12,b= "3"; var c=a*b;
After the "*" operation, C is 36. The operator "*" will convert the string implicitly to a number and then perform the operation.
"!" Operator:
var a= "111"; var b=!a;
After the "!" After the operation, B is false. Operator "!" The implicit quasi-change of its operand to a Boolean value is reversed.
Using the operator "!" This feature, can be very convenient through!! X is equivalent to Boolean (x) to convert X to a Boolean value.
2. Implicit type quasi-swap exists in the statement
If statement:
var obj = {name: ' Jack '}if(obj) { //do more}
An If statement converts the obj implicitly to a Boolean value
While statement:
var obj = {name: ' Jack '}while(obj) { //does more}
The while statement implicitly converts obj to a Boolean value
In statement:
var ary = [1,3,5,7]; for (var in ary) { typeof a);}
An implicit conversion from an identifier to a string occurs when an object literal is defined.
Two: Display type conversion
1. Forced type quasi-swap
The simplest way to do a display type conversion is to use: Boolean (), number (), String (), or oject () function
Boolean ():
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.
You can test a Boolean type cast with the following code snippet.
Boolean (""); // false – empty Boolean ("Hi"); // true – non-empty Boolean (+); // true – non-zero Boolean (null); // false - Boolean (0); // false - Boolean (new Object ()); // True – object
Number ():
The coercion type conversion of number () is similar to the parseint () and parsefloat () methods, except that it transforms the entire value, not the partial value. If the string value can be fully converted, number () will be judged to be
Call the parseint () method or call the Parsefloat () method, which returns Nan when the entire string value cannot be converted to a number.
Number (false) 0 number (true) 1 number (undefined) NaN Number (null) 0 "5.5") 5.5 "5.6.7") nan number (new Object ()) nan number ( 100)
String ():
It can convert any value into 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 1 to "1", and convert True to "true"
Convert false to "false", and so on. Except for null and any value other than undefinded has the ToString () property, the only difference between casting to a string and calling the ToString () method is that
Forcing a type conversion on a null or undefined value can generate a string without raising an error:
var S1 = String (null); // var onull = null; var S2 = onull.tostring (); // won ' t work , causes an error oject (): oject (3) // number (3)
Casting a null or undefined value to an object without throwing an exception simply returns a newly created empty object.
2. Conversion functions
JS provides two conversion functions for parseint () and parsefloat (). The former converts the value into an integer, which converts the value into a floating-point number. Only these methods are called on the string type, and the two functions work correctly;
The return for the other types is Nan (not a number).
parseint () and parsefloat () will parse the string carefully before judging whether the string is a numeric value. The parseint () method first looks at the character at position 0 and determines if it is a valid number;
The method returns Nan, and no further operations are performed. However, if the character is a valid number, the method will look at the character at position 1 and perform the same test. This process continues until a character is found that is not a valid number.
At this point parseint () converts the string before the character to a number.
parseint () and parsefloat () will skip any number of leading spaces, parse more numeric characters as much as possible, and omit the following.
parseint () can accept the second parameter, which specifies the cardinality of the number conversion, and the valid value range is 2~36.
parseint ("1234blue"); // returns parseint ("0xA"); // returns parseint ("22.5"); // returns parseint ("Blue"); // returns NaN
The Parsefloat () method is similar to the parseint () method, but for this method, the first decimal point that appears is a valid character. If there are two decimal points, the second decimal point will be considered invalid,
The Parsefloat () method converts the string before the decimal point to a number. This means that the string "22.34.5" will be parsed into 22.34. Another difference between using the Parsefloat () method is that the string must be
Represents a floating-point number in decimal form, not in octal or hexadecimal form. The method ignores the leading 0, so the octal number 0908 is parsed to 908. For hexadecimal number 0xA, the method returns Nan,
Because in floating-point numbers, X is not a valid character. In addition, parsefloat () has no base mode.
Parsefloat ("1234blue"); // returns Parsefloat ("0xA"); // returns Parsefloat ("22.5"); // returns Parsefloat ("22.34.5"); // returns Parsefloat ("0908"); // returns Parsefloat ("Blue"); // returns NaN
Type conversion of JS