Home >Web Front-end >JS Tutorial >Detailed explanation of js data type conversion issues

Detailed explanation of js data type conversion issues

迷茫
迷茫Original
2017-03-26 16:55:531373browse

##data type of js

##--> 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 conversion

var 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 string

You can convert other data types into strings

(1) String()

var a123;
a=String(a);
(2 )toString() method to convert (packaging class)

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 conversion

a) Convert number

var a="123";
a=+a;
Note

: +, -, *, /, % are all Strings can be implicitly converted to number

b) to String

var a=123;
a=a+" ";

c) to boolean

 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn