JavaScript Learning Notes Data Type _ basics

Source: Internet
Author: User
Tags numeric value hasownproperty

First, classification

Basic data type: Undefined, null, string, Boolean, number

Complex data type: Object

The properties of object are defined in the form of unordered names and value pairs (name:value)

Second, detailed

1. Undefined: Undefined type has only one value: Undefined, the value of this variable is undefined when the variable is declared with Var and uninitialized.

A variable that contains a undefined value is not the same as a variable that has not yet been defined, as the following example illustrates:

Copy Code code as follows:

var demo1;//declared but not initialized
alert (demo1);//undefined
alert (DEMO2);//error, DEMO2 is not defined

2, NULL: null type has only one value: null, from a logical point of view, a null value represents an empty object pointer.

If the defined variable is ready to be used to save the object in the future, it is best to initialize the variable to null instead of another value. This allows you to know whether the corresponding variable has saved a reference to an object by simply detecting the null value, for example:

Copy Code code as follows:

if (car!= null)
{
Perform some action on the car object
}

In fact, the undefined value is derived from a null value, so ECMA-262 stipulates that their equality test returns TRUE.

alert (undefined = null); True
Although null and undefined have such a relationship, their use is completely different. No matter under what circumstances it is not necessary to explicitly set the value of a variable to undefined, but the same rule does not apply to NULL. In other words, as long as the variable that is intended to save the object has not actually saved the object, you should explicitly let the variable save the null value. Doing so will not only embody null as the practice of null object pointers, but also help to further differentiate between null and undefined.

3. Boolean: Boolean type has two values: True and false, these two values are not the same as numeric values, so true is not necessarily equal to 1, and false is not necessarily equal to 0.

Note that Boolean literals are case-sensitive, that is, true and false (and other forms of case mixing) are not Boolean values, just identifiers.

Although there are only two Boolean literals, values of all types 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 Code code as follows:

var message = "Hello World";
var Messageasboolean = Boolean (message);

In this example, the string message is converted to a Boolean value that is saved in the Messageasboolean variable. You can call a Boolean () function on a value of any data type, and always return a Boolean value. Whether the returned value is TRUE or false depends on the data type of the value to be converted and its actual value. The following table shows the transformation rules for various data types and their objects.

These transformation rules are important for understanding flow control statements, such as if statements, to automatically perform corresponding Boolean transformations, such as:

Copy Code code as follows:

var message = "Hello World";
if (message)//is equivalent to if (Boolean (message) ==true)
{
Alert (' Value is true ');//value is True
}

Because of this automatic Boolean transformation, it is important to know exactly what variables are used in flow control statements.

4, Number: integers and floating-point

4.1 Integer: All octal and hexadecimal numbers are converted to decimal in the calculation

4.2 Floating point: The highest precision of a floating-point number is 17 bits, so the precision of the arithmetic is far less than the integer, for example: The result of 0.1+0.2 is not 0.3, but 0.30000000000000004. For example:

Copy Code code as follows:

a=0.2;
b=0.1;
if (a+b==0.3) {
Alert ("Hello");
}
else{
Alert ("HI");
}

The result pops up "hi", so do not test a particular floating-point number.

4.3 NaN: Non-numeric Not a number that represents a case in which the operand that would have returned a numeric value did not return a numeric value (so that it would not throw an error).

Nan itself has two unusual characteristics. First, any operation involving Nan (for example, NAN/10) returns Nan, a feature that can cause problems in multi-step computations. Second, Nan is not equal to any value, including the Nan itself. For example:

Copy Code code as follows:

Alert (nan = = Nan); False

JavaScript has a isNaN () function that takes a parameter that can be of any type, and the function will help us determine if the parameter is "not numeric." isNaN () after receiving a value, 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 a Boolean value. Any value that cannot be converted to a number will cause this function to return true. For example:

Copy Code code as follows:

Alert (isNaN (NaN)); True
Alert (isNaN (10)); False (10 is a numeric value)
Alert (isNaN ("10")); False (may be converted to numeric 10)
Alert (isNaN ("Blue")); True (cannot be converted to numeric values)
Alert (isNaN ("bule123")); Ture (cannot be converted to numeric values)
Alert (isNaN (true)); False (may be converted to numeric 1)

There are 3 functions that convert non-numeric values to numeric values: Number (), parseint (), and parsefloat (). The first function, the transition function number (), can be used with any data type, while the other two functions are specifically used to convert the string to a numeric value. These 3 functions return different results for the same input.

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

If this is a Boolean value, True and false are replaced with 1 and 0, respectively.

If it's a numeric value, just a simple pass in and return

If it is a null value, return 0

If it is undefined, return to Nan

If it is a string, follow these rules:

0 if the string contains only numbers, convert it to a decimal value, that is, "1" becomes 1, "123" becomes 123, and "011" becomes 11 (leading 0 is ignored)

0 if the 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)

0 if the string contains a valid hexadecimal format, such as "0xf", converts it to a decimal integer value of the same size

0 if the string is empty, convert it to 0

0 if the string contains characters other than the above format, convert it to Nan

If it is an object, the valueof () method of the object is called, and the value returned by the previous rule transformation is converted. If the result of the conversion is Nan, the object's ToString () method is called, and then the string value returned by the previous rule transformation is followed.

Copy Code code 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 more complex and unreasonable in converting strings, it is more commonly used when dealing with integers than the parseint () function, while the parsefloat () function is commonly used when dealing with floating-point numbers, as detailed in the following: http://www.cnblogs.com/ Yxfield/p/4167954.html

5, String

The string type is used to represent a sequence of characters consisting of 0 or more 16-bit Unicode characters, that is, a string. Strings can be represented by single quotes (') or double quotes (").

Copy Code code as follows:

var str1 = "Hello";
var str2 = ' Hello ';

The length of any string can be obtained by accessing its long property

Copy Code code as follows:

alert (str1.length); Output 5

There are two ways to convert a value to a string. The first is the ToString () method that uses almost every value.

Copy Code code 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. However, null and undefined values do not have this method.

In most cases, calling the ToString () method does not have to pass parameters. However, when you call the ToString () method of a value, you can pass a parameter: the cardinality of the output value.

Copy Code code 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)); A

This example shows that by specifying the cardinality, the ToString () method changes the value of the output. The value 10, depending on the cardinality, can be converted to a different numeric format at the time of the output.

You can also use the transformation function string () When you do not know if the value to convert is null or undefined, and this function can convert any type of value to a string. The String () function follows the following conversion rules:

If the value has the ToString () method, call the method (without parameters) and return the corresponding result

Returns "NULL" if the value is null

Returns "Undefined" if the value is undefined

6, Object

An object is actually a collection of data and functionality. An object can be created by executing the new operator followed by the name of the object type to be created. You can create a custom object by creating an instance of type object and adding properties and/or methods to it.

var o = new Object ();
The object type has any properties and methods that also exist in a more specific object, each instance of object having the following properties and methods:

Constructor (constructor)--Saves the function used to create the current object

hasOwnProperty (PropertyName)--for checking whether a given property exists in the current object instance (not in the prototype of the instance). Where the property name (PropertyName) as a parameter must be specified as a string (for example: O.hasownproperty ("name"))

isPrototypeOf (object)--Used to check if an incoming object is a prototype of another object

propertyIsEnumerable (PropertyName)--Used to check whether a given property can enumerate with for-in statements

ToString ()--Returns the string representation of an object

valueof ()--Returns a string, numeric, or Boolean representation of an object. Usually the same as the return value of the ToString () method.

Third, small test

Copy Code code as follows:

typeof (NaN)
typeof (Infinity)
typeof (NULL)
typeof (undefined)

A lot of interview will ask a few small questions above ~ ~

The above is the description of the 6 types of JavaScript data, the small partners are aware of it, hope that after reading this article can be improved.

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.