Basic concepts of JavaScript (iii) --- basic concepts of data type and javascript

Source: Internet
Author: User

Basic concepts of JavaScript (iii) --- basic concepts of data type and javascript
Data Type

There are five simple data types in Javascript, also known as basic data types, which areNull, Undefined, Boolean, Number, StringType. There is also a complex data typeObject, Object is essentiallyA group of unordered name-value pairs.

Data Type Detection

Javascript uses the typeof operator to detect specific data types.TypeofOperator, which may return a string:

I. If this value is undefined, undefined is returned;

II. If it is a boolean value, return boolean;

III. If it is a string, return string;

IV. If it is a value, return number;

V. If it is an object or null, the system returns the object;

VI. If it is a function, function is returned;

 

Undefined type

The undefined type has only one value, that is, undefined. When using the var operator to define a variable, the variable is not assigned a value, that is, it is not initialized. At this time, the value of the variable is undefined. The declared variable is the same as the value of undefined when it is initialized. You can refer to the following example:

 

As shown in the above example, for variables that are not initialized or initialized as undefined, the results are all undefined and the results are equal;

Let's take a look at the following example:

 

The preceding Declaration only declares the variable a1 and variable a2 is not declared. The result shows that the typeof operator is used, and undefined is returned for variables without declaration or initialization, however, an error is reported when no declared variables are printed directly. Therefore, you can initialize all declared variables. When the typeof operator returns undefined, this variable is not declared.

Null type

The Null type is the same as the Undefined type, and only one value is null. When the typeof operator is used to detect null values, the system returns the object, indicating that null is a Null object pointer, it can be explained based on the storage results of the reference type in the memory in the previous article,Reference objects in stack memory. The name in stack memory stores the variable name. The value storage in stack memory points to the heap memory path. A space in the heap memory is the real storage object space, null means that the variable name in the stack memory already exists, but the value in the stack memory does not point to any heap memory space, that is, a null object pointer..

As described above, if you want to declare a variable to save the object, it is best to initialize the variable to null instead of other values, in this way, you only need to check whether the variable is null to determine whether the variable stores an object reference.

If the null value is compared with the defined value, true is returned when the = Operator is used for comparison, but false is returned when the = Operator is used for comparison. As follows:

 

In this regard, ECMAScript deems that undefined is derived from null, so it is considered that they are defined as equal, but false is returned for full equality because the types of the two are inconsistent (typeof null returns object, typeof undefined returns undefined), which will be written later.

Boolean Type

The Boolean type has only two literal values:TrueAndFalseNote that javascript is case-sensitive and cannot be changed. If it is written as TRUE or True, it does not represent the boolean type. Although true can be converted to a numeric value of 1, false can be converted to a numeric value of 0, but true and false are not a concept of a numeric value, so true is not necessarily equal to 1, false is not necessarily equal to 0.

In Javascript, values of any type have two equivalent values of the Boolean type. You can use the Boolean () transformation function to convert the values. The conversion relationship is as follows:

Data Type

Convert to true

Convert to false

Boolean

True

False

String

Non-Null String

Null String

Number

Non-zero number

0 and NaN

Object

Any object

Null

Undefined

Not applicable

Undefined

 

Number Type

The Number type uses IEEF754 format to represent integers and floating-point numbers. There are three literal formats:Decimal integer, octal integer, and hexadecimal integerWhere the first digit of an octal integer must be 0. If the value in the literal value exceeds the value range, the leading 0 is ignored, and the subsequent values are parsed as decimal integers, the hexadecimal literal value must start with 0x followed by any hexadecimal number (0-9 and A-F), but when performing a data operation, all octal and hexadecimal data are converted to decimal for calculation.

The floating point in the Number type must beAt least one digit after the decimal pointIf the integer is 0, it can be omitted and not written. Although not recommended, it is indeed possible. BecauseThe memory space required to save the floating point number is twice that of the integer.Therefore, ECMAScript converts a floating point number to an integer for storage. Of course, it can be converted to an integer floating point number, such as 10.0. For a very large or extremely small floating point number, it can be expressed by scientific notation, that is, the value of a floating point number is equal to the value before e multiplied by the exponential power of 10. For example, 3.14e5 is equivalent to 3.14*105, if it is a very small floating point number, it is 3.14*10-5. By default, if there are more than 6 digits after the decimal point, it will be converted to scientific notation. In addition, due to the floating point number calculation problem of IEEF754, if you want to add two floating point numbers, the precision will be lost. For example, 0.1 + 0.2 is not equal to 0.3, but equal to 0.30000000000000004. ThereforeCannot test a specific floating point number.

Due to memory limitations, ECMAScript cannot store all values, and the maximum values that can be expressed are stored inNumber. MAX_VALUEThe minimum value is saved inNumber. MIN_VALUEIf a calculated value exceeds the javascript value range, if it is a positive number, it is Infinity. If it is a negative number, it is-Infinity, if Infinity exists, you cannot perform the next calculation. To determine whether a value is in the javascript value range, you can useIsFinite ()Function. If the value is in the range, true is returned; otherwise, false is returned.

There is a special value in javascript.NaN, IndicatesThe operand to return a value does not return a value.. In ECMAScript,If any value is divided by a non-numeric value, NaN is returned.. In addition, any operation involving NaN will return NaN, for example, 1 + NaN, then the return is NaN. Secondly, NaN is not equal to any value, including itself. Used in ECMAScriptThe isNaN () function determines whether any input parameter type is "not a value". After receiving the parameter, the function will try to convert the value of the parameter to a value. If the parameter can be converted to a value, for example, "10", false is returned. If the parameter cannot be converted to a value, for example, "asdf", true is returned.

In Javascript, three functions can be used to convert non-numeric values. The three functions areNumber (),

ParseInt () and parseFloat (). The Conversion Result of the Number () function is as follows:

I. If it is a Boolean value, true and false are converted to 1 and 0 respectively;

Ii. If it is a numeric value, the input is the same as the return value;

Iii. If the value is null, 0 is returned;

Iv. If it is undefined, NaN is returned;

V. If it is a string, follow the following rules:

A. If the string contains only numbers, convert it to a decimal number. For example, convert "-1" to-1 and "015" to 15.

B. If the string contains a valid floating point format, the corresponding floating point number is returned (the leading 0 is removed, and the positive and negative signs are taken ).

C. If the string contains a valid hexadecimal format, return the number after hexadecimal conversion to decimal.

D. If it is a Null String, 0 is returned.

E. If the string contains characters other than the preceding format, NaN.

F. If it is an object, call the valueOf () method of the object and convert the returned value according to the preceding rules. If the conversion result is NaN, call the toString () method of the object and convert the returned string value again according to the preceding rules.

When the parseInt () function is used for conversion, spaces in front of the string are ignored until the first non-null character. If the character is not a negative sign or number, NaN is directly returned, if it is a number or a negative number, it will continue to be parsed until it is completely parsed or contains non-numeric characters. For example, "1234aaa" will return 1234, "22.56" will return 22, because. it is not a numeric character. In addition, parseInt () can also recognize the octal or hexadecimal integer format. That is to say, if it starts with "0x" and is followed by a number character, it will be treated as a hexadecimal integer, same as octal. In addition, ECMAScript3 differs from ECMAScript5 in octal and hexadecimal conversion. For example, if the conversion is "070", parseInt ('070') in ECMAScript3 returns 56, however, in ECMAScript5, 70 is returned, which is converted in decimal format. To solve this problem, you can add a second parameter to the parseInt () function to indicate which method to convert, for example, parseInt ('070', 10) returns 70, parseInt ('070', 8), and 56.

The usage of parseFloat () is the same as that of parseFloat. There are two main differences: first, parseFloat can recognize ".", but it can only be the first decimal point. For example, the return value of "22.55.66" is 22.55, and the latter is discarded. Second, parseFloat always ignores the leading 0. parseFloat can identify all the floating point formats and decimal integers mentioned earlier. The hexadecimal string always returns 0.

In addition, if the string contains an integer that can be parsed (no decimal point or 0 after the decimal point), the return value of parseFloat is an integer.

String type

The String type consists of zero or multiple 16-bit Unicode characters. It is a character sequence, that is, a String. A string can be expressed in double quotation marks or single quotation marks. Strings expressed in double quotation marks are exactly the same as strings represented in single quotation marks. Therefore, either string can be used, but the two cannot be used together.

The length of a string can be obtained through the length attribute. Note that it is the length attribute rather than the length () method. The two cannot be confused.

The String data type contains some special literal characters, also known as escape sequences, which indicate non-printable characters or have other functions. The following are common escape characters:

Literal

Description

\ N

Line feed

\ T

Tabulation

\ B

Return

\ R

Enter

\ F

Paper Feed

\\

Slash

\'

Single quotes, which are used in strings expressed with single quotes

\"

Double quotation marks, used in strings represented by double quotation marks

The string in Javascript cannot be changed. That is to say, once a string is created, its value will not change. To change the string represented by a variable, destroy the original string, fill the variable with another string containing the new value.

There are two methods to convert a value to a string. The first method is to use the toString () method to return the string representation of the corresponding value. Values, Boolean values, and objects all have the toString () method, including the string itself, only the toSting () method of the string returns a copy of the string, but null and undefined do not have the toString () method. When the value type uses the toString () method, you can pass a parameter to return any string value in the valid hexadecimal format, such as number. toString (2) returns a string of the value in binary format. If no parameter is passed, the default value is a string in decimal format. The second method is to use the String () method. This method does not have to confirm whether the value to be converted is null or undefined. If the value is null, "null" is returned ", if the value is undefined, "undefined" is returned. If the value has the toString () method, this method is called to return the specific value. Of course, another conversion method is to use the plus sign operator to combine the value to be converted with a string.

 

Object Type

Objects in Javascript are actually a set of data and functions, or a set of unordered name-value pairs. An Object can be created by executing the new operator and the name of the Object type to be created. You can create a custom Object by creating an Object type example and adding attributes and methods to it. The creation method is as follows:

 

In the preceding three methods, you can create a person Object. The person, person1, and person2 are instances of the Object respectively. And java. lang. like an Object, the Object type in javascript is the basis of all its examples. That is to say, any attribute and method of the Object class also exists in its instance.

Each example of an Object has the following attributes and methods:

A. constructor: stores the function used to create the current object.

B. hasOwnProperty (propertyName): returns a Boolean value to check whether the current object has a corresponding attribute.

C. isPrototypeOf (Object): returns a Boolean value to check whether the specified Object exists in the prototype chain of another Object. For example, o1.isPrototypeOf (o2) indicates whether o1 exists in the prototype chain of o2, if yes, true is returned; otherwise, false is returned.

D. propertyIsEnumerable (propertyName): returns a Boolean value to check whether a given attribute can be enumerated using the for-in statement.

E. toLocaleString (): String Representation of the returned object, which corresponds to the region of the execution environment.

F. toString (): String Representation of the returned object.

G. valueOf (): returns the string, value, or Boolean value of the object. It is usually the same as the return value of the toString () method.

 

This is the data type part. We will try to write the operators, statements, and functions tomorrow, so that the basic part will be complete.

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.