Home > Web Front-end > JS Tutorial > body text

Detailed explanation of js data type conversion issues

迷茫
Release: 2017-03-26 16:55:53
Original
1324 people have browsed it

##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
Copy after login
var a="abc";
a=Number(a);
console.log(typeof a);     // NaN
Copy after login
var a=" ";
a=Number(a);
console.log(typeof a);    // 0
Copy after login

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
Copy after login
var a="    456467abasb";
a=parseInt(a);
console.log(a);    //456467
Copy after login
var a="   a123";
a=parseInt(a);
console.log(a);     //NaN
Copy after login
var a=123.12a=parseInt(a);
console.log(a);    //123
Copy after login

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);
Copy after login
(2 )toString() method to convert (packaging class)

var a=123;
a=a.toString();
Copy after login

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);
Copy after login

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;
Copy after login
Note

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

b) to String

var a=123;
a=a+" ";
Copy after login

c) to boolean

 a=123=!!a;
console.log(typeof a);    //true
a=!a; 
console.log(typeof a);    //false
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template