One, implicit conversion
During code execution, JavaScript transforms the data type as needed
Example a=nan;b=null;c=undefined;d= '; e=4;f= ' test ';
1. Convert to logical data type
You can view the converted results in the following ways
if (a) {
Alert ("NaN" converts the value of the logical type to true ");
}else{
Alert ("NaN" is converted to a logical type with a value of false ");
}
The results of the various examples:
nan=>false; null=>fasle; undefinded=>false; "' =>false;4=>true; ' Test ' =>true;
2. Convert to numeric type
You can view the converted results in a bit of a way
Alert ("Null converted to numeric result is" + (null+1-1));//result is 0;
The string cannot be converted by adding and subtracting the same number, because the string that appears in the ' + ' on both ends of JS converts the other type to a string
You can convert a string to number type by a plus or minus sign;
For example:
Alert ((+ '));//The result is 0;
Alert ((-"));//The result is 0;
Alert (+-' 234 ');//result is 234,-234;
Note: If the string contains non-numeric other characters, such as the letter special symbol will return Nan (nan means not a number a special type, it indicates that its data type is not numeric, and when the data is parsed if there is an error, it may only return a Nan for example: Pareint () $ . Parse_json (), etc.);
Such as:
Alert (+ ' 23test ');//result is Nan;
The results of the various examples:
true=>1; false=>0; null=>0; undefined=>nan; "' =>0; ' Test ' =>nan; ' 1234 ' =>1234; ' -1234 ' =>-1234; nan=>nan;
3. Convert to Characters
Conversion to a string is relatively simple, directly to the required type plus an empty string on it, what the original value, the value of the conversion is unchanged, here is not much to say
Summary: Remember that a special value is converted to a value after the conversion, such as NULL to convert the logical type to false to a number type of 0 to convert to a string of ' null ';
Second, display type conversion
To prevent the undesirable consequences of automatic conversion or non-conversion, we can manually convert the types that need to be converted primarily through some built-in functions
Commonly used in the main has
parseint (), parsefloat (), convert the string type to the corresponding shape or float, convert from the first character of the string backward, and stop the conversion if the word to be converted identifier to a non-numeric type; As parseint (' 23ab32 ') result for 23;parseint (' SDD ') result is Nan; (Global object)
toLowerCase () touppercase (); converts letters to lowercase, uppercase, or string objects, respectively
var c= ' SDFDS ';
Alert (C.tolowercase ());
toString () is converted to string type; (becomes a String object after conversion)
JavaScript data types, implicit and display transformations