##--> Basic types (value types): Number, Stringstring, Boolean
##-->Composite type (Reference type): Object(Array, Time type Date, FunctionType Function, Regular expressionRegExp...)
-->Empty type: Null、Undefined##1: Data type The conversion of
1 shows the conversion of
##A. Convert numbers:
If you want to convert a string type data into a number, you can use:
(1) Number conversion:
var a="123"; a=Number(a); console.log(typeof a); // number
var a="abc"; a=Number(a); console.log(typeof a); // NaN
var a=" "; a=Number(a); console.log(typeof a); // 0
Note: ①If the converted content itself is a numeric type string, then its own number type will be returned during future conversion (special case: true returns 1 and false returns 0)
② If the converted content itself is not a numerical string, the result during conversion will be NaN③ If the converted content is an empty string (null), then the conversion result will be 0
④If it is another string, the future conversion result will be NaN (2) ParseInt conversionvar a="123"; a=parseInt(a); console.log(typeof a); //number
var a=" 456467abasb"; a=parseInt(a); console.log(a); //456467
var a=" a123"; a=parseInt(a); console.log(a); //NaN
var a=123.12a=parseInt(a); console.log(a); //123
Note
: ①Ignore the beginning of the string spaces, until the first non-empty character is found, the non-numeric string after the number will also be removed ② If the first number is not a numeric symbol or a negative sign, NaN## will be returned # ③ Decimals will be rounded (rounded down) (3)parseFloat Floating point number (decimal)
Same as parseInt, the only difference is that parseFloat can retain decimals
B. Convert to stringYou can convert other data types into strings
(1) String()var a123;
a=String(a);
var a=123; a=a.toString();
Note
: null and undefined do not have toString method, all types of String can be converted
C. Convert to Boolean typeYou can convert other types into boolean values Boolean()
var a="true"; a=Boolean(a);
Note: Converting , all content will result in true after conversion, except: false, " " (empty string), 0, NaN, null, undefined, ""
##2. Implicit conversiona) Convert numbervar a="123";
a=+a;
b) to String
var a=123; a=a+" ";
a=123=!!a; console.log(typeof a); //true a=!a; console.log(typeof a); //false
The above is the detailed content of Detailed explanation of js data type conversion issues. For more information, please follow other related articles on the PHP Chinese website!