JS Advanced Programming--data type

Source: Internet
Author: User

There are 5 simple data types (also known as basic data types) in ECMAScript: Undefined, Null, Boolean, number, string and object, symbols;
typeof determines the type of data returned undefined, Boolean, number, String, object (if this value is an object or null), function;

Note: typeof is an operator and not a function, so the parentheses in the example are not required, although they can be used;

Null

If a defined variable is intended to be used to hold an object in the future, it is better to initialize the variable to null instead of to another value. This way, you can know if the corresponding variable has saved a reference to an object as long as you check the null value directly

In fact, the undefined value is derived from a null value, so ECMA-262 specifies that the equality test for them returns true

Alert (Null = = undefined); True

Boolean

A value that converts the value of the data type to true to False
Boolean true False
String any non-empty string "" (empty string)

Number any non-0 numeric value (including infinity) 0 and NaN

Object NULL for any objects

Undefined Undefined

Number

Because the memory space required to hold the floating-point value is twice times the value of the saved integer, ECMAScript will lose no chance in converting the floating-point value to an integer value

Obviously, if the decimal point is not followed by any number, then this value can be saved as an integer value. Similarly, if the floating-point value itself represents an integer (such as 1.0), then the value is also converted to an integer

NaN, that is, a non-numeric (not a number) is a special value that represents a case where an operand that would have returned a numeric value does not return a number (so that no error is thrown).

The NaN itself has two unusual features

1. Any operation involving Nan (for example, NAN/10) will return Nan, which may cause problems in multi-step computation;

2, Nan and any value are not equal, including Nan itself;

For these two characteristics of NaN, ECMAScript defines the IsNaN () function. This function takes a parameter, which can be any type, and the function will help us determine whether the parameter is "not a value". IsNaN () after receiving a value, it attempts to convert the value to a number. Some values that are not numeric are converted directly to numeric values, such as the string "10" or the Boolean value. And any value that cannot be converted to a number causes the function to return true, in short, to hate numbers;

numeric conversions

The conversion rules for the number () function are as follows

? If it is a Boolean value, True and false are converted to 1 and 0, respectively.
? If it is a numeric value, it is simply passed in and returned.
? If it is a null value, 0 is returned.
? If it is undefined, return NaN.
? If it is a string, follow these rules:

If the string contains only numbers (including cases preceded by a plus or minus sign), it is converted to a decimal value, that is, "1" becomes 1, "123" becomes 123, and "011" becomes 11 (note: The leading 0 is ignored);

If the string contains a valid floating-point format, such as "1.1", it is converted to the corresponding floating-point value (also ignoring the leading 0);

If the string contains a valid hexadecimal format, such as "0xf", it is converted to a decimal integer value of the same size;

If the string is empty (contains no characters), it is converted to 0;

If the string contains characters other than the above format, it is converted to NaN.

If it is an object, the ValueOf () method of the object is called, and the returned value is converted according to the preceding rule. If the result of the conversion is NaN, the object's ToString () method is called, and then the returned string value is converted again according to the previous rule.

parseint ();

The parseint () function converts a string more to see whether it conforms to a numeric pattern. It ignores the spaces in front of the string until the first non-whitespace character is found. If the first character is not a numeric character or a minus sign, parseint () returns Nan, that is, converting an empty string with parseint () returns Nan (number () returns 0 for a null character). If the first character is a numeric character, parseint () continues to parse the second character until all subsequent characters have been parsed or a non-numeric character is encountered. For example, "1234blue" is converted to 1234 because "blue" is completely ignored. Similarly, "22.5" is converted to 22 because the decimal point is not a valid numeric character. If the first character in a string is a numeric character, parseint () can also recognize various integer formats (the decimal, octal, and hexadecimal numbers discussed earlier). That is, if the string starts with "0x" and is followed by a numeric character, it is treated as a hexadecimal integer, and if the string starts with "0" followed by a numeric character, it is parsed as an octal number.

var num1 = parseint ("1234blue"); 1234
var num2 = parseint (""); NaN
var num3 = parseint ("0xA"); 10 (hexadecimal number)
var num4 = parseint (22.5); 22
var num5 = parseint ("070"); 56 (octal number)
var num6 = parseint ("70"); 70 (decimal number)
var num7 = parseint ("0xf"); 15 (hexadecimal number)

Paresfloat ();

Parsefloat () also parses each character starting with the first character (position 0). It is also parsed until the end of the string, or until an invalid floating-point numeric character is encountered. That is, the first decimal point in the string is valid, and the second decimal point is invalid, so the string following it is ignored. For example, "22.34.5" will be converted to 22.34.

parsefloat () parses only the decimal value , so it does not specify the usage of the cardinality with the second argument;

If the string contains a number that can be resolved to an integer (no decimal point, or 0 after the decimal point), parsefloat () returns an integer;

String type

JS Advanced Programming--data type

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.