JavaScript data types

Source: Internet
Author: User

This article is based on JavaScript advanced programming three.

ECMAScript divides data into two categories: basic data Types and complex data types . There are 5 types of basic data types (also known as simple data types): Undefined, Null, Boolean, number, String. There are only 1 types of complex data: Object. So the value will eventually be one of the 6 data types mentioned above.

Given that ECMAScript is loosely typed, a means is needed to detect the data type of a given variable-typeof is the operator that provides this information. typeof is an operator, so the parentheses below are not required. Using typeof on a value may return one of the following strings:

    • "Undefined"--if this value is not defined;
    • "Boolean"--if this value is a Boolean value;
    • "Number"--if the value is numeric;
    • "String"--if the value is a string;
    • "Object"--if the value is an object or null, note that typeof null returns "Object";
    • "Function"--if the value is a function;

Some people will have doubts about the function here, there is no function in the data type! According to the high School (JavaScript Advanced Program design three, the unified abbreviation for the senior three) on the function from a technical point of view in the ECMAScript is an object, not a data type. However, the function does have some special properties, so it is necessary to differentiate it and other objects by using the TypeOf operator.

1.Undefined type

The undefined type has only one value undefined. When a variable declaration is not initialized, the value of this variable is undefined.

2.Null type

Null is the second type with only one value, and this special value is null. From a logical point of view, a null value represents an empty object pointer, so typeof returns "Object" when NULL is detected.

The undefined value is derived from a null value, so ECMA-262 fixed undefined = = NULL returns true, but = = = returns false.

3.Boolean type

The Boolean type has two values: True and the value in False.ecmascript can be called Boolean () to correspond to a Boolean value. The following table shows the various data types and their corresponding conversion rules:

  

Understanding these rules is important for understanding if self-executing, such as:

var msg= "Hello";

if (msg) {

Alert ("MSG is a non-null character, automatically converted to true in If");

}

4.Number type

The number type uses IEEE754 to represent integers and floating-point values. In order to support various numeric types, ECMA-262 defines different numeric literal formats, octal, decimal, and hexadecimal.

1) floating point number

The ECMA to those enormous and minimum values is expressed in scientific notation, for example:

0.000003//3e-6 3000000//3e6

Floating-point numbers have the highest precision of 17 bits, and here's a more interesting one:

0.1+0.2 = = 0.3//false Because 0.3 true value is 0.30000000000000004

2) range of values

Due to memory limitations, ECMAScript cannot save the world's values. Ecmasscript can indicate that the minimum value is saved in Number.min_value, which is 5e-324 in most browsers, and the maximum value that can be represented is saved in Number.MAX_VALUE. In most browsers, this value is 1.7976931348623157e308, if more than two values are represented as Infinity (positive infinity) and-infinity (negative infinity).

Isfinite () can be used to detect whether a value is poor, and the value returns true between Number.min_value and Number.MAX_VALUE.

3) NaN

NaN (not a number) is a special numeric value, which is used to indicate that an operand that would have returned a numeric value does not return a value (only if it is not mistaken), for example:

Other languages do not allow the divisor to be 0,ecmascript but can, any value divided by 0 returns Nan, so the program can continue execution. Any computation that involves Nan returns Nan.

Nan = = Nan//false itself is not equal

ECMAScript defines the isNaN function for the two major characteristics of ECMAScript. This function accepts an arbitrary parameter to help us determine that the parameter is not a numeric value. When the parameter is accepted, isNaN attempts to convert the argument from a value that is not a number to a numeric value, and returns true if it cannot be converted to a value. A Boolean, numeric character, "" can be converted to a number that returns false, and the other returns true.

4) Numeric conversion

There are three functions that convert a non-numeric value to a value: Number (), parseint (), parsefloat (). The parameter of number () can be any data type, and the latter two can only accept string arguments.

The conversion rules for number () are as follows:

  

Because the number () function is complex and unreasonable when converting strings, the parseint () function is more commonly used when working with integers

⑴ if the first non-null character is not a numeric value or a minus sign, Nan is returned.

such as: parseint ("abc")//nan

⑵ if the first non-null character is a numeric value, continue parsing until parsing is complete or a non-numeric character is encountered
such as: parseint ("123")//123 parseint ("123abc")//123 parseint ("22.5")//22

⑶parseint () can also recognize various integer formats (octal, decimal, hexadecimal). In ECMAScript3, the first character of 0 is interpreted as octal parsing, which is ignored in ECMAScript5, such as example 1; To avoid this divergence, parseint () provides a second argument so that it can be shown to convert a parameter to the corresponding integer format as in Example 2:

Example 1:parseint ("070"); ECMASCRIPT3 parseint ("070"); ECMASCRIPT5 70

Example 2:parseint ("070", 8)//ECMASCRIPT3 parseint ("070", 8)//ECMASCRIPT5 70

⑷ the first 0x is treated as a hexadecimal parsing. Such as:

parseint ("0xAF", +)//175 parseint ("0xAF")//175

⑸ if the first character is not a numeric value, but the parseint () function passes the second parameter, it can also parse normally, such as:

parseint ("AF", 16)//175

Sometimes we want to convert floating-point numbers, so the parsefloat () function can help us keep decimals. This function is also parsed from the first character until the parsing is complete or a non-numeric character is encountered. Parseint and parsefloat have two differences, the first parsefloat can parse a decimal, and the second parsefloat always ignores the leading 0. However, a 16 binary string always resolves to 0, and since parsefloat can only parse the decimal integer format, there is no second argument. It is also important to note that for example: 22.0 will be parsed into an integer 22. Parsefloat can parse scientific counting method E,parseint can't.

such as: parsefloat (' 3e2 ')//300 parseint (' 3e2 ')//3

5.String type

The string type is used to represent a sequence of 0 or more 16-bit Unicode characters, that is, a string. There are 3 ways to convert to characters: 1.toString () 2.String () 3.+ "". When you do not know the variable type is using Method 1 conversion will error, because null and undefined do not have ToString (). Tosting () can also transmit parameters (2, 8, 10, 16)

6.Object type

The object in Ecmasctipt is actually a combination of data and functions.

There are two ways to create it: 1. var obj= new Object (); 2.var obj = {}

JavaScript data types

Related Article

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.