JavaScript learning Notes: Data Types and javascript learning notes

Source: Internet
Author: User
Tags hasownproperty

JavaScript learning Notes: Data Types and javascript learning notes

I. Classification

Basic data types: undefined, null, string, Boolean, and number

Complex data type: object

Object Attributes are defined in the form of unordered names and value pairs (name: value ).

Ii. Details

1. undefined: Undefined type has only one value: undefined. When the variable is declared but not initialized using var, the value of this variable is undefined.

The variables that contain the undefined value are different from those that are not yet defined. The following example shows:

Copy codeThe Code is as follows:
Var demo1; // declaration but not initialization
Alert (demo1); // undefined
Alert (demo2); // error, demo2 is not defined

2. null: The null type has only one value: null. Logically, the null value indicates a null object pointer.

If the defined variable is to be used to save the object in the future, it is better to initialize the variable to null instead of other values. In this way, as long as the null value is detected, you can know whether the corresponding variable has saved an object reference. For example:

Copy codeThe Code is as follows:
If (car! = Null)
{
// Perform some operations on the car object
}

In fact, undefined values are derived from null values, so the ECMA-262 specifies that true is returned for their equality tests.

Alert (undefined = null); // true
Although null and undefined have such a relationship, their usage is completely different. No matter under any circumstances, it is unnecessary to explicitly set the value of a variable to undefined, but the same rule is not applicable to null. In other words, as long as the variable to save the object has not actually saved the object, it should be explicitly made to save the null value of the variable. This will not only reflect the Convention that null acts as a null Object Pointer, but also help to further differentiate null and undefined.

3. Boolean: Boolean has two values: true and false. These two values are not the same as numerical values. Therefore, true is not necessarily equal to 1, and false is not necessarily equal to 0.

Note that the nominal values of the Boolean type are case-sensitive. That is to say, True and False (and other forms of case-sensitive mixture) are not Boolean values, but only identifiers.

Although there are only two literal values of the Boolean type, all types of values in JavaScript have values equivalent to these two Boolean values. To convert a value to its corresponding Boolean value, you can call the type conversion function Boolean (), for example:

Copy codeThe Code is as follows:
Var message = 'Hello world ';
Var messageAsBoolean = Boolean (message );

In this example, the message string is converted to a Boolean value, which is saved in the messageAsBoolean variable. You can call the Boolean () function for values of any data type, and a Boolean value is always returned. Whether the returned value is true or false depends on the Data Type of the value to be converted and the actual value. The following table shows the conversion rules for various data types and their objects.

These conversion rules are very important for understanding how flow control statements (such as if statements) automatically execute corresponding Boolean conversions. For example:

Copy codeThe Code is as follows:
Var message = 'Hello world ';
If (message) // equivalent to if (Boolean (message) = true)
{
Alert ("Value is true"); // Value is true
}

Because of this automatic Boolean conversion, it is critical to know exactly what variables are used in a flow control statement.

4. number: integer and floating point

4.1 INTEGER: During calculation, all octal and hexadecimal numbers are converted to decimal.

4.2 floating point: the highest precision of a floating point value is 17 bits. Therefore, the accuracy of a floating point value is far inferior to that of an integer in arithmetic calculation. For example, the result of 0.1 + 0.2 is not 0.3, but 0. 30000000000000004. For example:

Copy codeThe Code is as follows:
A = 0.2;
B = 0.1;
If (a + B = 0.3 ){
Alert ("hello ");
}
Else {
Alert ("hi ");
}

The result displays "hi", so do not test a specific floating point value.

4.3 NaN: the non-value Not a Number. This value is used to indicate a situation where the operand that originally wanted to return the value has Not returned the value (so that no error will be thrown ).

NaN has two extraordinary characteristics. First, any operation involving NaN (such as NaN/10) will return NaN, which may cause problems in multi-step computing. Secondly, NaN is not equal to any value, including NaN itself. For example:

Copy codeThe Code is as follows:
Alert (NaN = NaN); // false

JavaScript has an isNaN () function. This function accepts a parameter, which can be of any type, and the function will help us determine whether the parameter is "not a value ". After receiving a value, isNaN () tries to convert the value to a value. Some non-numeric values are directly converted to numeric values, such as the string "10" or Boolean value. Any value that cannot be converted to a value will cause this function to return true. For example:

Copy codeThe Code is as follows:
Alert (isNaN (NaN); // true
Alert (isNaN (10); // false (10 is a value)
Alert (isNaN ("10"); // false (may be converted to a value of 10)
Alert (isNaN ("blue"); // true (cannot be converted to a value)
Alert (isNaN ("bule123"); // ture (cannot be converted to a value)
Alert (isNaN (true); // false (may be converted to value 1)

There are three functions that can convert non-numeric values to numeric values: Number (), parseInt (), and parseFloat (). The first function, that is, the transformation function Number (), can be used for any data type, and the other two functions are specifically used to convert a string to a value. These three functions return different results for the same input.

The conversion rules of the Number () function are as follows:

● If it is a Boolean value, true and false will be replaced with 1 and 0 respectively.

● If it is a numeric value, it is simply passed in and returned

● If the value is null, 0 is returned.

● If it is undefined, NaN is returned.

● If it is a string, follow the following rules:

○ If a string contains only numbers, convert it to a decimal value, that is, "1" will be 1, "123" will be 123, and "011" will change to 11 (leading 0 is ignored)

○ If a string contains a valid floating point format, such as "1.1", it is converted to the corresponding floating point number (similarly, leading 0 is ignored)

○ If a string contains a valid hexadecimal format, for example, "0xf", convert it to a decimal integer of the same size.

○ If the string is empty, convert it to 0.

○ If a string contains characters other than the preceding format, convert it to NaN

● If it is an object, call the valueOf () method of the object and convert the returned value according to the previous rules. If the conversion result is NaN, call the toString () method of the object, and convert the returned string values in sequence according to the preceding rules.

Copy codeThe Code is as follows:
Var num1 = Number ("Hello World"); // NaN
Var num2 = Number (""); // 0
Var num3 = Number ("000011"); // 11
Var num4 = Number (true); // 1

Because the Number () function is complex and not reasonable enough to convert strings, parseInt () is a commonly used function when processing integers. parseFloat () is often used to process floating point numbers () function, see: http://www.cnblogs.com/yxField/p/4167954.html

5. String

The String type is used to represent a String that consists of zero or multiple 16-bit Unicode characters. A string can be expressed by single quotation marks (') or double quotation marks.

Copy codeThe Code is as follows:
Var str1 = "Hello ";
Var str2 = 'hello ';

The length of any string can be obtained by accessing its length attribute.

Copy codeThe Code is as follows:
Alert (str1.length); // output 5

There are two methods to convert a value to a string. The first method is to use the toString () method that almost every value has.

Copy codeThe Code is as follows:
Var age = 11;
Var ageAsString = age. toString (); // string "11"
Var found = true;
Var foundAsString = found. toString (); // string "true"

Values, Boolean values, objects, and string values all have the toString () method. But null and undefined values do not have this method.

In most cases, parameters are not required to call the toString () method. However, when calling the toString () method of a value, you can pass a parameter: The base number of the output value.

Copy codeThe Code is as follows:
Var num = 10;
Alert (num. toString (); // "10"
Alert (num. toString (2); // "1010"
Alert (num. toString (8); // "12"
Alert (num. toString (10); // "10"
Alert (num. toString (16); // ""

In this example, we can see that by specifying the base, the toString () method will change the output value. The value 10 can be converted to different numeric formats during output based on the different base numbers.

If you do not know whether the value to be converted is null or undefined, you can also use the transformation function String (), which can convert any type of value to a String. The String () function follows the following conversion rules:

● If the value has a toString () method, call this method (without parameters) and return the corresponding result.

● If the value is null, "null" is returned"

● If the value is undefined, "undefined" is returned.

6. object

Objects are actually a set of data and functions. 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 instance of the Object type and adding its attributes and (or) methods.

Var o = new Object ();
Any attributes and methods of the object type also exist in more specific objects. each instance of the Object has the following attributes and methods:

● Constructor (constructor) -- stores the function used to create the current object.

● HasOwnProperty (propertyName) -- used to check whether a given property exists in the current object instance (rather than in the instance's prototype. The parameter property name must be specified as a string (for example, o. hasOwnProperty ("name "))

● IsPrototypeOf (object) -- used to check whether the input object is a prototype of another object

● PropertyIsEnumerable (propertyName) -- used to check whether a given attribute can be enumerated using the for-in statement.

● ToString () -- returns the string representation of the object

● ValueOf () -- returns the string, value, or Boolean value of the object. It is usually the same as the return value of the toString () method.

Iii. Small Test

 Copy codeThe Code is as follows:
Typeof (NaN)
Typeof (Infinity)
Typeof (null)
Typeof (undefined)

Many interviews will ask the above few small questions ~~

The above is an introduction to these six types of javascript data. Do you know this? I hope you can improve it after reading this article.

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.