Data types and conversions in JS

Source: Internet
Author: User

The data type in JS

The data type in JS can be divided into five types: number, String, Boolean, Underfine, null.

Number: Numeric type, integral type floating point type is included.

String type, right numeric alphabetic string, and punctuation, must be enclosed in single or double quotes.

Boolean: Boolean type, with only true and false values of two.

Underfine: Undefined, generally refers to a variable that has been declared but has no assignment, such as Var A;

Null: null object type, var a = null, and var a= "" There is a difference;

Object: An object is a data entity that sets together a number of properties and methods that are related to each other, in the common Window,document,array of JS.

NaN: He is a special value that represents not a numeric value, which can be understood as a special type of number, and only if prompted when data type conversion occurs in the JS operation, the IsNaN () method is special to judge the data, if the number returns false, not the number returns TRUE.

Ii. Conversion of data types

In JS, data type conversion is generally divided into two types, that is, forced type conversion and implicit type conversion (using JS weak variable type conversion).

Cast:

Using JS provided by the function parseint (), parsefloat (), number (), Boolean () for data conversion, as the name implies, the first two is the data to parse the conversion, the former is an integer, the latter is a floating-point number. The principle of their interpretation is to analyze the past and try to do what they can. If there is an identifiable number on the analysis, if the first digit is not the number of the return nan.number is to judge the whole, is a digital return number, otherwise Nan.boolean () is the existence and true and false judgment.

parseint ():

parseint ("123");//123

parseint ("+123");//123

parseint ("-123");//123

parseint ("12.3 yuan")//12

parseint ("abc");//nan

parseint ([up])//1

parseint ([1,2,4])//1

parseint ("");//nan

The method also has a base mode that converts binary, octal, hexadecimal, or any other binary string into integers. The base is specified by the second parameter of the parseint () method, so to parse the hexadecimal value, the direct parse for the number that contains the preamble requires the following call:

parseint ("AA", 16);//170

parseint ("10", 2);//2

parseint ("10", 8);//8

parseint ("10", 10);//10

Contains leading

parseint ("0xAA")//170

If the binary number contains leading 0, it is best to use cardinality 2, otherwise the default is to parse in decimal

parseint ("010"); Returns 10

parseint ("010", 2),//2 parseint ("010", 8);   Returns 8 parseint ("010", 10); Returns ten

parseint (NULL)//nan

Parsefloat ():

Parsefloat ("123");//123

Parsefloat ("-123");//123

Parsefloat ("+123");//123

Parsefloat ("12.34");//12.34

Parsefloat ("12.35 yuan")//12.35

Parsefloat ("12.23.122");//12.23

parsefloat ("AV");//nan

Parsefloat ("0xAA");//0

Parsefloat ("0110");//110

Parsefloat ([1]);//1

Parsefloat ([2,3]);//2

Parsefloat ([]);//nan

parsefloat (NULL)//nan

Number ():

alert (123);//123

Number ("123");//123

Number ("+123");//123

Number ("12.3");//12.3

Alert (12.3 yuan) ;//false

Number (true);//1

Number ("12.3.4");//nan

Number ("");//0

Number ("ABC");//nan

Number ([]);//0

Number ([1]);//1

Number ([up]);//nan

Number (new Object ());//nan

Number (null);//0

Boolean (): He does not automatically convert the numbers inside the quotation marks.

Boolean (1);//true

Boolean (0);//false

Boolean ("1");//true

Boolean ("0");//true

Boolean ("abc");//true

Boolean (");//false

Boolean (");//true

Boolean ([])//true

Boolean ([1])//true

Boolean (NULL)//false

A string () is the conversion of all data types passed into a string.

The difference between the and ToString () methods is that

typeof String (NULL)//string

typeof String (undefined)//string

Implicitly-Typed conversions:

Implicit type conversion is very different in Java, the data type in JS is not strict, there is no floating point and integer type, here the implicit type conversion refers to the conversion between the string and the numeric type, when the string and the number is reduced multiplication modulo operation or comparison operation, he will automatically convert the string to a number. The default method for converting numbers is to call number (), and the addition operation is to stitch the numbers as strings. var x = "123";

var y = 121;

alert (x+y);//"123121;

Alert (x-y);//2

alert (x*y);//14883

alert (x/y);//1.016528256198346

alert (x%y);//2

alert (x>y);//true

alert (x==y);//false

Alert ("123a" >y);//false weird.

JS only a weak type of scripting language, the syntax relative to the Java and other high-level programming language is not strict enough, so for its data type and conversion between the easy to confuse, the above is only a experience, if there are errors, welcome to point out!

Data types and conversions in

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.