JavaScript Advanced Programming (2nd edition)

Source: Internet
Author: User

3.4 Data types

ECMAScript has 5 simple data types, also known as basic data types, Undefined, Null, Boolean, number, and string, as well as a complex data type--object.

3.4.1 typeof operator

typeof is used to detect the data type of a given variable, and using the TypeOf operator on a value may return one of the following strings:

"Undefined"--if this value is undefined

"Boolean"--if this value is a Boolean value

"String"--if this value is a string

"Number"--if the value is numeric

"Object"--if this value is an object or null;

"Function"--if the value is a function l;

3.4.2 Undefined type

The undefined type has only one value, that is, a special undefined. When you declare a variable with VAR but do not initialize it, the value of the variable is undefined.

For example:

1 var  2 alert (typeof//undefined3//true

In addition, using typeof for undefined variables, the return value is also undefined

Example: 1 alert (typeof FF); // undefined

3.4.3 NULL type

The null type also has only one value, that is, the Null,null value represents an empty object pointer, and this is why "object" is returned when using the TypeOf operator to detect null values, for example:

var NULL  ; Alert (typeof//Object

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 the other 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.

if NULL {  // perform certain actions  on the car object }

3.4.4 Boolean type

The Boolean type has 2 literals: true and false.

Calling the Boolean () function for any data type always returns a Boolean value. For example:

var message = "some string"; var messageasboolean = Boolean (message);

As to whether the returned value is TRUE or false, depending on the data type of the value to be converted and the actual value, the various data types and their corresponding conversion rules are given below:

Data type The value converted to true Value converted to False
Boolean True False
String Any non-empty string "" (empty string)
Number Any non-0 numeric value (including infinity) 0 and Nan
Object Any object Null
Undefined N/A (not application not applicable) Undefined

var message = "some string"; if (message) { alert ("Value is true");}

3.4.5 Number Type

var // decimal var // octal var // invalid octal, resolved to 79 (value in literal value is out of range, leading 0 is ignored) var // Decimal Ten

1. Floating-point values

Because the memory space required to hold a floating-point value is twice times the value of the saved integer, ECMAScript will lose no chance to convert the value to an integer value. For example:

var // no digits after the decimal point, resolved to 1 var // the floating-point value itself represents an integer, and the change is converted to an integer and resolved to

e notation, i.e. scientific counting method

var // 31250000 var // 0.0000003

Never test a specific floating-point value, for example:

var a = 0.05; var b = 0.25; if // never do such a test.    Alert ("true");} Else {    alert ("false");}//The output is False

2. Range of values

Due to memory limitations, ECMAScript cannot save all the values in the world, and the smallest value ECMAScript can represent is saved in Number.min_value, which is 5e-324 in most browsers. The maximum value that can be represented is saved in Number.MAX_VALUE, which is 1.7976931348623157e+308 in most browsers. If the result of a calculation is given a value that is outside the range of JavaScript values, if the value is negative, it is converted to-infinity (negative infinity), and if the value is an integer, it is converted to Infinity (positive infinity). The Isfinite () function can detect whether a value is poor.

JavaScript Advanced Programming (2nd edition)

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.