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 name and value pairs (name:value)
Second, detailed
1, undefined:undefined type has only one value: Undefined, when using VAR to declare a variable but uninitialized, the value of this variable is undefined.
Variables that contain undefined values are not the same as those that have not yet been defined, as the following example illustrates:
var demo1;//Statement 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 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 directly detect the null value, for example:
if (car! = null) { //Perform some action on the car object }
In fact, the undefined value is derived from a null value, so ECMA-262 specifies that the equality test for them returns true.
alert (undefined = = null); True
Although null and undefined have such relationships, their use is completely different. In any case, 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 hold the object does not actually hold the object, you should explicitly let the variable hold the null value. Doing so not only manifests null as a convention for null object pointers, but also helps to further differentiate between null and undefined.
3. The Boolean:boolean type has two values: True and false, which is not the same as the numeric value, so true is not necessarily equal to 1, and false is not necessarily equal to 0.
It is important to note that Boolean literals are case-sensitive, that is, true and false (as well as other forms of case blending) are not Boolean values, just identifiers.
Although there are only two literals of the Boolean type, the 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:
var message = ' Hello world '; var Messageasboolean = Boolean (message);
In this example, the string message is converted to a Boolean value that is stored in the Messageasboolean variable. You can call the Boolean () function on a value of any data type, and always return a Boolean value. The value returned is true or false, depending on the data type of the value to be converted and its actual value. The following table shows the conversion rules for various data types and their objects.
These translation rules are important for understanding flow control statements, such as if statements, to automatically perform a corresponding Boolean conversion, such as:
var message = ' Hello world '; if (message)//equals if (Boolean (message) ==true) { alert ("Value is true");//value is True
}
Because of this automatic Boolean conversion, it is important to know exactly what variables are used in flow control statements.
4, Number: integer and floating point
4.1 Integers: All octal and hexadecimal numbers are converted to decimal when the calculation is performed
4.2 Floating point: The highest precision of a floating-point value is 17 bits, so it is much less accurate than an integer when arithmetic is calculated, for example: The result of 0.1+0.2 is not 0.3, but 0.30000000000000004. For example:
a=0.2; b=0.1; if (a+b==0.3) {alert ("Hello");} Else {alert ("HI");}
The result is "HI", so don't test a particular floating-point value.
4.3 NaN: Non-numeric not a number, which is used to denote a case where an operand that would have returned a numeric value did not return a numeric value (so that no error is thrown).
The Nan itself has two unusual features. First, any operation involving Nan, such as NAN/10, will return Nan, a feature that can cause problems in a multi-step calculation. Second, Nan is not equal to any value, including the Nan itself. For example:
Alert (nan = = nan); False
There is a isNaN () function in JavaScript that takes a parameter that can be of any type, and the function will help us determine if 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. Any value that cannot be converted to a number will cause the function to return true. For example:
Alert (IsNaN (NaN)); True alert (IsNaN (ten)); False (10 is a numeric value) alert (IsNaN ("ten")); False (may be converted to a value of ten) alert (IsNaN ("Blue")); True (cannot be converted to numeric value)
Alert (IsNaN ("bule123")); Ture (cannot be converted to numeric) alert (IsNaN (true)); False (may be converted to a value of 1)
There are 3 functions that can convert non-numeric values to numeric values: Number (), parseint (), and parsefloat (). The first function, the transformation function number (), can be used for any data type, while the other two functions are specifically used to convert a string into a numeric value. These 3 functions return different results for the same input.
The conversion rules for the number () function are as follows:
If it is a Boolean value, True and false are replaced with 1 and 0, respectively
If it is a numeric value, simply pass in and return
If it is a null value, returns 0
If it is undefined, return Nan
If it is a string, follow these rules:
0 If the string contains only numbers, it is converted 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 (also ignoring the leading 0)
0 If the string contains a valid hexadecimal format, such as "0xf", it is converted 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 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 in turn by the preceding rule.
var num1 = number ("Hello world"); NaN var num2 = number (""); 0 var num3 = number ("000011"); One -by-one var num4 = number (true); 1
Since the number () function is more complex and unreasonable when converting strings, it is more commonly used when working with integers than the parseint () function, while the parsefloat () function is commonly used when dealing with floating-point numbers, see: 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, the string. The string can be represented by a single quotation mark (') or double quotation mark (").
var str1 = "Hello"; var str2 = ' Hello ';
The length of any string can be obtained by accessing its long property
alert (str1.length); Output 5
There are two ways to convert a value to a string. The first is the ToString () method, which uses almost every value.
var age = one; var ageasstring = age.tostring (); The string "one" var found = true; var foundasstring = found.tostring (); String "true"
Numeric, Boolean, object, and string values have the ToString () method. However, the null and undefined values do not have this method.
In most cases, calling the ToString () method does not have to pass arguments. However, when you call the ToString () method of a number, you can pass a parameter: the cardinality of the output value.
var num = ten; Alert (num.tostring ()); "Ten" Alert (num.tostring (2)); "1010" Alert (num.tostring (8)); alert (num.tostring); "Ten" alert (num.tostring); 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 output.
You can also use the Transform function string (), which can convert any type of value to a string, without knowing that the value to be converted is null or undefined. The String () function follows the following translation rules:
If the value has the ToString () method, the method (with no arguments) is called and the corresponding result is returned
Returns "NULL" if the value is null
Returns "Undefined" if the value is undefined
6. Object
An object is actually a set of data and functions. 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 ();
Any properties and methods that the object type has also exist in more specific objects, and each instance of object has the following properties and methods:
Constructor (constructor)--holds the function used to create the current object
hasOwnProperty (PropertyName)--Used to check for the existence of a given property in the current object instance, rather than in the prototype of the instance. where the attribute 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 use the For-in statement to enumerate
ToString ()--Returns the string representation of an object
ValueOf ()--Returns the string, numeric, or Boolean representation of the object. Usually the same as the return value of the ToString () method.
Third, small test
typeof (NaN)
typeof (Infinity)
typeof (NULL)
typeof (undefined)
A lot of interviews will ask the above few small questions ~ ~
Learn JavaScript from the beginning (c)--data type