Data type and type conversion of JS

Source: Internet
Author: User

Js There are six types in the data type , number ,string,boolean,underfine,null, Object.

In general, we will use the typeof () method to determine the data type of the variable.

Conversion of Data is divided into explicit type conversions and implicit type conversions , which we often use to convert variables into strings or numbers.

One: explicit type conversions :

1. When converting to a string:

ToString()

1 var str= abc;2 alert (str.tostring ());

2. When converting to a number:

(1)parseint () converted to integers,parsefloat () converted to floating-point numbers. Example:

1 var test = parseint ("blue");//returns NaN 2 3 var test = parseint ("1234blue");//returns 1234 4 5 var test = parseint ("22.5");//returns 6 7 var test = parsefloat ("1234blue");//returns 1234 8 9 var test = parsefloat ("22.5"); Returns 22.5

(2) Number();

Example: Number ("678") results in 678

3. Convert to Boolean type:

Boolean () ;

Example: Boolean ("AAA") results are true

Two: implicit type conversions:

1. Convert a number to a string:

num+ ':

1 var num = 1.23456;2 alert (typeof (num+//string 

2. Convert strings into numbers:

(1)(-*/% can allow string to be implicitly converted to number) Example:

1 var str= ' 14.7 '; 2 alert (str*1+2); 16.7

(2)+ +-- become a digital

3. Convert to Boolean type: ! Inverse , turn the data type on the right to a Boolean value.

Three: Additional test examples for data conversion methods are as follows:

Example Results
parseint (' 1234 ') 1234
parseint (' 1234.00 ') 1234
parseint (' 1234abc ') 1234
parseint (' abc1234 ') Undefined (conversion failed)
parsefloat (' 1234.123 ') 1234.123
parsefloat (' 1234.123a ') 1234.123
parsefloat (' a1234.123 ') NaN
Number (' 1234.123 ') 1234.123
Number (' 1234.123AA ') NaN
String (eval (' 12+10 ')) 22
Boolean (' 0 '), Boolean (' 567 '), Boolean (567) True
Boolean (NULL), Boolean (False), Boolean (0), Boolean ("), Boolean () False
This.tostring () [Object]
(typeof (This)). ToString () Object
Eval (' 12+34 ') 46
Eval (' 12+34 ') + ' 46
Eval (' 12+34 ') *1.0 46
typeof (Eval (' 12+34 ') *1.0) Number
typeof (Eval (' 12+34 ') + ') String

Data type and type conversion of JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.