There are 5 data types in javascript: undefined, null, Boolean, number, String, and typeof to detect the data type.
1 undefined:undefined There is only one value, that is Undefined, when declaring a variable with VAR but not initializing it, this variable is Undefined.
Note: The typeof returned for undeclared variable execution is also undefined.
2 Null:null also has only one value, Null. From a logical point of view, NULL represents an empty object pointer. So typeof null returns an object. In fact, undefined is derived from a null value, so null ==undefined returns TRUE.
3 Boolean: There are two values, true and false. The literal value of Boolean literals True and false is case-sensitive. That is, True and False are not Boolean values, just identifiers. You can call the Boolean () function on a value of any data type, and always return a Boolean value.
var flag = "Hello world!"; var msg = Boolean (flag);
The conversion rules for various data types and Boolean types are described in the following table:
Data type |
Convert to True |
Convert to False |
Boolean |
True |
False |
String |
Any non-empty string |
"" Empty string |
Number |
Any value other than 0, including infinity |
0 and Nan |
Object |
Any non-empty object |
Null |
Undefined |
N/A (not applicable not applicable) |
Undefined |
According to the above rules, we can write the following code example:
function test () { var flag = "Hello world!"; if (flag) { alert ("Write:true"); } else{ alert ("Write:false");} }
4 Number: The IEEE754 format is used to represent integers and floating-point numbers, and in order to support various numeric types, ECMA-262 defines different numeric literal formats.
var intnum = 55;//decimal integer
var octalnum = 070;//Octal 56, base 8, octal literal is worth the first bit must be 0, then the octal number sequence 0~7, if the literal value is out of range, then the leading 0 will be ignored, the back vertical will be treated as a decimal resolution.
var hexnum = 31 of the 0x1f;//16 binary, the first two must be 0x, followed by a sequence of hexadecimal digits with 0~9 and a~f (which can be uppercase and lowercase).
(1) floating point number: Can be expressed by scientific notation, 3.125e7, 3e-7. The highest precision of a floating-point value is 17 decimal, but it is much less accurate than an integer in arithmetic calculations. For example, the result of 0.1 plus 0.2 is not 0.3, but 0.30000000000000004. This small rounding error can result in the inability to test a specific floating-point value. For example
if (A + b ==0.3) {
Alert ("You got 0.3.");
}
In this example, we are testing for two numbers and is not equal to 0.3. If these two numbers are 0.05 and 0.25, or 0.15 and 0.15 are not a problem, but if 0.1 and 0.2, then the test will not pass. Therefore, never test a particular floating-point value. (The problem of floating-point calculation error is the common problem of floating-point computation based on IEEE754 numeric value, so there are other languages too.) )
(2) Numerical range: 5e-324 to 1.7976931348623157e+308. If the value is out of range, it is automatically converted to a special Infinity (-infinity) value, at which point the value cannot continue to participate in the next calculation. To determine whether a value is not infinite, you can use the Isfinite () function.
(3) NaN: A non-numeric value that represents a case where an operand that would have returned a numeric value did not return a number, such as any number divided by 0 to return NaN. Any operation involving Nan (NAN/10) will return a nan,nan that is not equal to any value, including the Nan itself. The IsNaN () can be used to determine if it is not a value, and IsNaN () also applies to the object.
(4) Numeric conversions: Number (), parseint (), parsefloat (). Number (): Can be used for any data type. parseint (), parsefloat (): specifically used to convert a string to a numeric value. Three functions will 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 converted to 1 and 0 respectively;
If it is a number, it is simply passed in and returned;
If NULL, returns 0;
If it is undefined, return nan;
If it is a string, follow the rules below:
If the string contains only numbers (including the preceding sign), it is converted to a decimal value, such as "011" becomes 11 (leading 0 is ignored);
If the string contains a valid floating-point format, it is converted to a floating-point value (leading 0 is ignored);
If the string contains a valid hexadecimal, such as "0xf", it is converted to the same size decimal integer value;
If the string is empty (does not contain any characters), it is converted to 0;
If the string contains characters other than the above, it is converted to Nan;
If it is an object, call the ValueOf () method, which is converted according to the preceding rule.
parseint (): When converting a string, the function is more to see if it conforms to the numeric pattern. It ignores the spaces in front of the string until the first non-null character is found. If the first non-null character is not a numeric character or minus sign, it returns NaN, which is parseint ("") =nan, while number ("") =0,parseint (' 1234blue ') =1234,parseint (' 22.5 ') = 22. parseint (' 0xAF ', +) =parseint (' AF ', 16), the second parameter specifies hexadecimal.
Parsefloat (): Similar to parseint (), the difference is that the first decimal point in the string is valid, the following is invalid, and it only parses the decimal, not the second argument.
5 string: The type contains some special characters, the escape sequence.
Methods to convert to strings:
ToString (): Numeric, Boolean, object, string value have this method, but null, undefined value does not have this method, the method can pass parameters, that is, binary, decimal, etc., num.tostring (10);
String (): The other type is the same as ToString (), but null returns "NULL" and undefined returns "undefined".
6 Special Type: Object
Create: var o = new Object ();
Constructor constructor: such as Object ();
Detects if a property exists hasOwnProperty (): O.hasownproperty ("name");
ToString ();
ValueOf ();
Data types in JavaScript