Javascript has five simple data types: undefined, null, Boolean, number, and string, and a complex data type-object.
Typeof Operator
The typeof operator can be used to determine the Data Type of a given variable. The following string may be returned:
Undefined ---- variable undefined or unassigned Value
Boolean ------ Boolean Value
String -------- string
Number ------ Value
Object ------- object or null
Function ----- function (the function is an object rather than a data type, but the function has some special attributes, so it is differentiated)
The operand of the typeof operator can be a variable or a constant such as 34. typeof is an operator rather than a function. Therefore, it can be enclosed by parentheses or not.
Undefined type
The undefined type has only one value, that is, the special undefined. When a variable is declared with VAR but not initialized, the value is undefined, and it is not initialized to undefined. For undeclared variables, if typeof is used to view their types, it is also undefined, but the variables that declare unassigned values are different from those that are not declared, if you use alert to output a variable that is not declared, an error occurs. You can only perform the typeof operation on the variable that is not declared.
Null type
The null type is the same as the undefined type and has only one value. It is null. Logically, null indicates a Null Object Pointer. Therefore, if typeof is used to detect its type, an object is returned.
The undefined value is derived from the null value, so the equal operator (=) between null and undefined returns true. But because of its different types, the full equality operator (=) returns false.
Boolean
The boolean type has only two values: true and false. Case sensitive.
You can call the Boolean () function to convert a value to its corresponding Boolean value. Conversion rules:
If the value is of the string type, convert a non-null string to true, and convert a null string to false.
If the value is of the number type, convert non-zero values to true, and convert 0 and Nan to false.
If the value is of the object type, convert non-null values to true, and convert null values to false.
If the value is of the undefined type, convert it to false.
Boolean conversion is automatically executed when you want to test the if condition.
Number Type
The default number type is decimal. You can add a leading 0 to indicate octal, and a leading 0x to indicate hexadecimal.
Values can be expressed in octal or decimal notation, but they are converted to decimal values during arithmetic computation.
If the integer is 0 or 0, the floating point value can be omitted. For example, 0.1 can be written. 1, but it is best to write for clarity. If the decimal part is null or 0, it is converted to an integer. When there are too many decimal places, you can use e notation, that is, the first is a number, the middle is E or E, followed by a 10 power, for example, 3.1e5 indicates 310000, by default, it is automatically converted to e notation when there are six zeros or more after the decimal point.
Number. min_value indicates the minimum value that can be expressed.
Number. max_value indicates the maximum value that can be expressed.
If the value exceeds the Javascript value range, it is automatically converted to infinity or-infinity (negative infinity). isfinite can be used to indicate whether a number is poor.
Nan indicates a non-a number, which is used to indicate that a numeric value is not returned in an operation to return the numeric value (to prevent throwing an error ). Nan is returned for any operation involving Nan. Nan is not equal to any value, including Nan itself. Nan = Nan returns false. The isnan function tries to convert the input value to a number. If the conversion fails, true is returned; otherwise, FASE is returned. Isnan () is also applicable to objects. It calls the valueof () method of the object and determines whether the value returned by this method can be converted to a value. If the valueof () method is not defined, the tostring () method is called (this is the general execution process of built-in functions and operators in ecmascript ).
There are three functions that can convert non-numeric values to numeric values: Number (), parseint (), and parsefloat ().
Number () function conversion rules:
If it is a Boolean value, true is converted to 1, false is converted to 0;
If it is null, convert it to 0;
If it is undeined, convert it to Nan;
If it is a string, follow the following rules:
If only a number is included, it is converted to the corresponding decimal number, which can recognize the hexadecimal format. For example, "0x11" is converted to 17, but octal is not recognized. Leading 0 is ignored, for example, "011" is converted to "11;
If only the valid floating point format is included, it is converted to the corresponding floating point value (the leading 0 is ignored );
If only valid hexadecimal format is included, convert it to a decimal integer of the same size;
Convert an empty string to 0;
If it contains characters other than the preceding format, it is converted to Nan.
If it is an object, the valueof () of the object is called and then converted according to the previous rules. If the valueof () method is undefined (that is, its implementation is inherited from the default implementation of the Object ), the tostring () method of the object is called and then converted according to the preceding rules.
Parseint () function conversion rules:
The parseint () function ignores spaces before the string during conversion until the first non-space character is found. If the first character is not a number or a negative number, Nan is returned (the Nan, number () function returns 0 if an empty string is converted). If the first character is a numeric character, the subsequent characters are parsed until all subsequent characters are parsed or a numeric character is encountered. For example, "3434dfdf" is converted to 3434. parseint () can recognize octal and binary hexadecimal, if parseint ("070") returns 56, parseint ("0xf") returns 15.
You can add a second parameter to the parseint () function to indicate the base of the conversion. For example, parseint ("10", 2) is converted to 2, and the default value is 10.
Parsefloat () function conversion rules:
Similar to the parseint () function, it is parsed to an invalid character (the first decimal point is valid, and the second decimal point is invalid). If the string contains a number that can be parsed as an integer, parsefloat () an integer is returned. If parsefloat () is not parsed to the octal value, leading 0 is ignored. If it is a hexadecimal value, 0 is always returned.
String type
The string type is immutable. When you change the string saved by the variable, the original string is destroyed, and then the variable is filled with another string containing the new value.
Except null and undefined, each value has a tostring () method. You can call the tostring () method of the value to return a string representation of the value. By default, the numeric format returns its decimal representation, but other hexadecimal representations can be output by passing the base parameter, such as VAR num = 10; alert (Num. tostring (2); "1010" is returned ".
The string () function can also convert values to strings. The conversion rules are as follows:
If the value has the tostring method, the tostring () method is called;
If the value is null, return "null ";
If the value is undefined, "undefined" is returned ".
The string () function can only return the decimal value.
Object Type
Create an object by executing the new operator followed by a function name. In this case, this function is a constructor. Create an object-type instance and add attributes and methods to it to create a custom object, when parameters are not passed to the constructor, brackets after the function can be omitted. The object type is the basis of all other objects. The attributes and methods of the object type also exist in other more specific objects. Each object instance has the following attributes and methods:
Constructor ---- Save the function for creating the current object;
Hasownproperty (propertyname) ---- check whether the given property is in the instance of the current object (not in the prototype );
Isprototyoeof (object) ---- check whether an object is a prototype of an object;
Propertyisenumerable (propertyname) ---- check whether the given attribute can be enumerated using the for-in statement;
Tostring () ---- returns the string representation of the object;
Valueof () ---- return the string, value, or Boolean value of the object.
An online test question
1 Alert ( Typeof (NAN ));
2 // Number
3 Alert ( Typeof (Infinity ));
4 // Number
5 Alert ( Typeof ( Null ));
6 // Object
7 Alert ( Typeof (Undefined ));
8 // Undefined
9 Alert (Nan = NAN );
10 // False
11 Alert (Nan ! = NAN );
12 // True
13 Alert (Nan > NAN );
14 // False
15 Alert ( Null = Undefined );
16 // True, undefined extends null
17 Alert ( Null > = Undefined );
18 // False
19 Alert ( Null <= Undefined );
20 // False
21 Alert ( Null = Null );
22 // True
23 Alert ( Null ! = Null );
24 // False
25 Alert ( Null ! = NAN );
26 // True
27 Alert ( Null = NAN );
28 // False
29 Alert (Nan = Undefined );
30 // False
31 Alert (parseint ( " 123abc " ));
32 // 123
33 Alert ( " 123abc " - 0 );
34 // Nan
35 Alert (infinity > 10 );
36 // True
37 Alert (infinity > " ABC " );
38 // False
39 Alert (infinity = NAN );
40 // False
41 Alert ( True = 1 );
42 // True
43 Alert ( New String ( " ABC " ) = " ABC " );
44 // True
45 Alert ( New String ( " ABC " ) === " ABC " );
46 // False
47
48 Function STEP (){
49
50 Return Function (X ){
51
52 Return X + A ++ ;
53
54 }
55
56 }
57
58 VaR A = STEP ( 10 );
59
60 VaR B = STEP ( 20 );
61
62 Alert (( 10 ));
63 // 20
64 Alert (B ( 20 ));
65 // 40
66 VaR A = " 123abc " ;
67
68 Alert ( Typeof ( ++ ));
69 // Number
70 Alert ();
71 // Nan