Data types are the foundation of all the complex abstractions you can imagine. In modern programming languages, apart from some simple data types built in the language itself, basically, a language mechanism is provided for customizing data types (struct can also be used in C ), to a certain extent, these mechanisms determine the popularity and vitality of the language. ECMAScript is a dynamic language, built on five simple data types (Undefined, Null, Boolean, Number, String) and one complex data type (Object. This article will review simple data types. I will try to describe them from the perspective of Programming Practice. The following code runtime environment is FireFox 14.0.1.
Simple data type
Simple data type |
Value |
Undefined |
Undefined (only one value) |
Null |
Null (only one value) |
Boolean |
True | false (only two values are supported) |
Number |
Value |
String |
String |
In ECMAScript, the preceding five simple data types, Boolean, Number, and String all have built-in packaging objects with the same name, the literal value (variable) of the simple data type is automatically packed according to the situation, so that the method can be called directly. For the specific methods that can be called, we will explain in detail when discussing the built-in objects:
The Code is as follows:
Console.info (true. toString (); // true, equivalent to packaging with Boolean () and then calling
Lele.info (Boolean (false). toString (); // false, convert false to a Boolean Value
Console.info (new Boolean (false). toString (); // false, use Boolean () to wrap false.
Console.info (false. toString (); // false, equivalent to packaging with Boolean () and then calling
Console.info ('test'. toString (); // test, equivalent to packaging with String () and then calling
Try {
Lele.info (undefined. toString (); // no corresponding packaging type, throwing an exception
} Catch (e ){
Lele.info (e); // TypeError
}
Try {
Lele.info (null. toString (); // no corresponding packaging type, throwing an exception
} Catch (e ){
Lele.info (e); // TypeError
}
Var num = 4;
Console.info (num. toString (); // 4. You can call a method directly on a simple numeric variable, which is equivalent to calling a method after being packaged using Number ().
// Lele.info (3. toString (); // SyntaxError. syntax errors cannot be captured using try, which means they cannot be directly called using a numeric literal.
Next, let's talk about the most commonly used data conversions:
(1) convert to Boolean :!! Value
(2) convert to Number: + value
(3) convert to String: ''+ value
Five Simple data types are described below:
1. Undefined type
The Undefined data type has only one value: undefined.
(1) All uninitialized values are undefined by default (it is unnecessary to initialize a variable as undefined ).
(2) In the function scope, the function parameter that does not pass in the actual parameter is undefined.
(3) When the function does not explicitly return or return; the return value is undefined.
(4) In ECMAScript, specify null = undefined to return true, while null = undefined to return false.
(5) the corresponding Boolean value of undefined is false.
(6) When typeof is used to act on undefiend, the return string 'undefined' will also return 'undefined' when acting on a variable that has never been declared '.
(7) The undefined conversion value is NaN, and the conversion string is 'undefined '.
The Code is as follows:
Console.info (undefined === undefined); // true
Lele.info (typeof undefined); // undefined
Console.info (typeof noDefined); // undefined, undefined identifier. If typeof is used, undefined is returned. This is also the unique and available operator for undefined identifiers.
Console.info (! Undefined); // true, true is returned here, which is the basis for determining whether a variable is initialized in many condition statements.
Console.info (!! Undefined); // any value, double negative !! It is converted to the corresponding Boolean value. Here, the corresponding Boolean value of undefiend is false.
Console.info (undefined = null); // according to ES, the return value for the equality test between null and undefined is true.
Console.info (undefined = null); // However, after all, undefined and null are two data types. When full comparison is used, false is returned.
Console.info (typeof undefined = undefined); // false, typeof undefined returns a string 'undefined', so false is output here
Console.info (+ undefined); // NaN, the value is NaN
Lele.info (''+ undefined); // undefined, converted to the string 'undefined'
2. Null type
The Null type also has only one value: null.
(1) If typeof is used for null values, the 'object' string is returned '.
(2) the corresponding Boolean value of null is false.
(3) If a variable is used to save an object, it is recommended that the initialization be null.
(4) convert null to 0 and convert string to 'null '.
The Code is as follows:
Console.info (null = null); // true
Lele.info (typeof null); // object
Console.info (! Null); // true
Console.info (!! Null); // false, which indicates that the corresponding Boolean value of null is false.
Console.info (undefined = null); // true
Console.info (undefined === null); // false
Console.info (+ null); // 0, to 0
Lele.info (''+ null); // null, converted to the string 'null'
3. Boolean type
The Boolean type has only two values: true and false.
(1) Although there are only two values, values of any data type can be converted to corresponding Boolean values. There are three conversion methods:
A. Convert using the transformation function Boolean ()
Note that when Boolean () is used as a conversion function, it is converted to a corresponding Boolean value. When it is used as a constructor, an object is created, the Boolean value of any non-empty object is true, which may cause misunderstanding. We recommend that you do not use Boolean (). A similar situation exists for String () and Number.
B. double denial !! Operator Conversion
C. implicit conversion, for example, in some conditional statements
(2) true and false of the Boolean type. When typeof is used, the 'boolean' string is returned '.
(3) When converting to a value, true and false are converted to 1 and 0 respectively. When converting to a string, they are 'true' and 'false' respectively '.
The Code is as follows:
Var value = 'test ';
Var empty = '';
Console.info (!! Value); // true
Console.info (!! Empty); // false
Console.info (Boolean (value); // true
Console.info (Boolean (empty); // false
Console.info (!! Value = Boolean (value); // true, which indicates that the two conversion methods are equivalent.
Console.info (!! Empty = Boolean (empty); // true
Console.info (new Boolean (value); // Boolean object. Note that new is used to create an object.
Console.info (new Boolean (empty); // Boolean object
If (value) {// implicitly convert to true
Console.info (value); // test
}
If (empty) {// implicitly convert to false without executing the internal statements in parentheses
Console.info ('empty ');
}
If (new Boolean (empty) {// create an object first, and then implicitly convert it to true. The statement in parentheses is executed.
Console.info ('empty'); // empty
}
Console.info (typeof true = 'boolean'); // true
Console.info (+ true); // 1, unary operator, converted to a value of 1
Console.info (+ false); // 0
Console.info (''+ true); // true, + operator after overload, converted to string 'true'
Lele.info (''+ false); // false
The specific conversion rules are as follows:
Data Type |
Convert to true |
Convert to false |
Undefined |
- |
Undefined |
Null |
- |
Null |
Boolean |
True |
False |
Number |
Any non-zero value (including infinity) |
0 and NaN |
String |
Any non-empty string |
Null String |
Object |
Any object |
- |
4. Number Type
In ECMAScript, there is no separate integer or floating point type, and there is only one Number type, which is expressed in the IEEE754 format (this representation has a rounding error during computation). Here we will not elaborate on the underlying implementation, these things have a headache when learning C language at school. Below I will put the most used in actual programming at the beginning, which is generally enough. For friends who do not want to be troubled by the details of too edges, they can skip the subsequent discussion about Number at any time.
(1) Numerical Conversion: mainly the following three conversion functions
- Number () function: similar to Boolean (), the function converts data to the Number type, which is the same as the one-dimensional plus operator (+). We recommend that you use the + operator, which is relatively simple.
- ParseInt () function: parses integers. data can be imported and imported. For example, parseInt ('070', 8) Outputs a decimal 56 value.
- ParseFloat () function: parses floating point numbers and can only accept one parameter. Note that if the parsed data result is an integer, an integer is directly returned.
Note: When Number () and + are used for conversion, true-> 1, false-> 0, undefined-> NaN, null-> 0, empty string-> 0, non-empty string-> parses by Numeric value.
The Code is as follows:
Var trueVal = true;
Var falseVal = false;
Var undef = undefined;
Var nullVal = null;
Var intVal = '1 ';
Var floatVal = '1. 0 ';
Var strVal = 'test ';
Var empty = '';
Console.info (Number (trueVal); // 1
Console.info (Number (falseVal); // 0
Console.info (Number (undef); // NaN
Console.info (Number (nullVal); // 0
Console.info (Number (intVal); // 1
Console.info (Number (floatVal); // 1
Console.info (Number (strVal); // NaN
Console.info (Number (empty); // 0
Console.info (+ trueVal); // 1
Console.info (+ falseVal); // 0
Console.info (+ undef); // NaN
Console.info (+ nullVal); // 0
Lele.info (+ intVal); // 1
Console.info (+ floatVal); // 1
Console.info (+ strVal); // NaN
Console.info (+ empty); // 0
Console.info (parseInt (trueVal); // NaN
Console.info (parseInt (falseVal); // NaN
Console.info (parseInt (undef); // NaN
Console.info (parseInt (nullVal); // NaN
Console.info (parseInt (intVal); // 1
Console.info (parseInt (floatVal); // 1
Console.info (parseInt (strVal); // NaN
Console.info (parseInt (empty); // NaN
Console.info (parseFloat (trueVal); // NaN
Console.info (parseFloat (falseVal); // NaN
Console.info (parseFloat (undef); // NaN
Console.info (parseFloat (nullVal); // NaN
Console.info (parseFloat (intVal); // 1
Console.info (parseFloat (floatVal); // 1
Console.info (parseFloat (strVal); // NaN
Console.info (parseFloat (empty); // NaN
Note: the behavior of these conversion functions may vary according to the different implementations of the browser. We recommend that you write and test the functions if you have any questions during the actual programming process. In JavaScript advanced programming (version 3rd), this section describes many different results than what I actually run. For example, the original book says parseInt () can only parse strings, however, the following code can also be run:
The Code is as follows:
Var object = {
Value: 1,
ToString: function (){
Return this. value;
}
};
Console.info (parseInt (object); // 1
(2) integers and floating-point numbers: If you are influenced by the C language, you must differentiate between integers and floating-point numbers! In ECMAScript, they are not as different as expected. Simply put, there must be at least one digit after the decimal point, that is, a floating point, or an integer, for example, 1.01 is a floating point, 1. 1.00 because there is no number not 0 after the decimal point, the engine will resolve it to an integer 1. You may imagine that the result of division of two integers will also be rounded up, for example, 3/2 = 1. But in ECMAScript, do not worry about this. It has already restored its mathematical attributes, you will find 3/2 = 1.5, which will be mentioned again in the operator-related part.
(3) hexadecimal: This is also called the Carry System, which is actually the carry (low to high) method. Each base has a base. When the low value reaches this base, in the upper-right corner, enter 1. In daily life, the most commonly used is the decimal system. For example, if the decimal angle is 1 yuan, there is also a 24-digit System (24 hours is 1 day) in the time measurement) and 60 hexadecimal (60 seconds is 1 minute). In ancient times, there were also hexadecimal (think about it ). However, in computer processing, because the current is only in two states, so only binary data can be processed, but this is not easy for natural persons to understand, therefore, the hexadecimal and hexadecimal values are used as the intermediate states of the hexadecimal and binary conversions.
In ES3, octal, decimal, and hexadecimal can be used. However, in ES5, octal is disabled.
Octal: starts with 1 digit 0, followed by an octal digital sequence (0 ~ 7) if the number exceeds 7, the leading 0 is ignored and processed as a 10-digit system. For example, 08 is parsed as a 10-digit 8.
Hexadecimal: It starts with 1 digit 0 and 1 letter x, followed by a hexadecimal sequence (0-9a-fA-F ).
Decimal: You can directly write all digits one by one, or use scientific notation (do not understand? Look for a middle school mathematics textbook.
(3) special value: In ECMAScript, there are two special values NaN and Infinity. The former indicates that it is Not a value (Not a Number ), the latter indicates a value that is not in the specified range, and a positive or negative sign can be used to represent the direction. For these two special values, we will not examine the specific operation rules here (if you are interested, you can test it yourself. I will also give some examples below). We will only explain the following two values:
A. You cannot use val = NaN to determine whether A variable is NaN. Instead, you must use the global isNaN () function. This function accepts A parameter, true is returned when the parameter can be converted to a value. Otherwise, false is returned.
B. Use the global isFinite () function instead of val = Infinity to determine whether the value is out of the range. The function accepts a parameter and returns true if the value of the parameter is within the range indicated, otherwise, false is returned. The range indicates the Number. MIN_VALUE to Number. MAX_VALUE. In addition, there is an attribute Number in Number. NEGATIVE_INFINITY and Number. POSITIVE_INFINITY, whose values are Infinity and-Infinity.
The Code is as follows:
Console.info (0/0); // NaN
Console.info (NaN = NaN); // false
Console.info (NaN + 1); // NaN
Console.info (NaN/NaN); // NaN
Var notNumber = NaN;
Console.info (notNumber = NaN); // false
Console.info (isNaN (notNumber); // true
Console.info (1/0); // Infinity
Console.info (-1/0); //-Infinity
Console.info (1/Infinity); // 0
Console.info (Infinity/Infinity); // NaN
Console.info (Infinity = Infinity); // true
Var inf = Infinity;
Console.info (inf = Infinity); // true
Console.info (! IsFinite (inf); // true
Console.info (! IsFinite (NaN); // true
Console.info (isNaN (Infinity); // false
Note: In JavaScript advanced programming (version 3rd), page 29th says that if any value is divided by 0, NaN will be returned. In fact, this is not the case.
5. String type
Unlike the General C language, there is no character type in ECMAScript, and the String type is regarded as a simple type. The literal value is enclosed by quotation marks (single quotation marks or double quotation marks.
(1) For String operations, the plus sign "+" is overloaded. If any value is added to the String, it is first converted to a String using String, then merge the two strings.
(2) String () conversion, undefined-> 'undefined', null-> 'null', true-> 'true', false-> 'false ', value Type: Number-> visible numeric character conversion, Object-> call toString.
The Code is as follows:
Console.info (''+ 1 + 1); // 11 instead of 2
Console.info (''+ true); // true
Lele.info (''+ undefined); // undefined
Console.info (''+ null); // null
(3) The character string is escaped using the Backslash "". Some common escape characters include:
Literal |
Description |
Literal |
Description |
\ N |
Line feed |
\\ |
Backslash |
\ T |
Tabulation |
\' |
Single quotes |
\ B |
Space |
\" |
Double quotation marks |
\ R |
Enter |
\ Xnn |
A character expressed in hexadecimal code nn. |
\ F |
Paper Feed |
\ Unnnn |
A Unicode character in The hexadecimal code nnnn. |
Now, we can sort the simple data types here.