There are 5 basic data types in ECMAScript: Undefined, Null, Bollean, number, and string,1 complex data types: Object.
Because ECMAScript is loosely typed, it provides an operator-typeof that detects the data type of a given variable. Using this operator on a value may return one of the following strings:
"Undefined"-if this value is undefined
"Boolean"-if this value is a Boolean value
"String"-if this value is a string
"Number"-if this value is numeric
"Object"-if this value is an object or null
"Function"-if this value is a function
L Undefined Type
This type has only one value of-undefined. This variable is undefined when declaring a variable with VAR but not initializing it.
Example: Var age;
alert (age);//"Undefined"
Alert (typeof age);//"Undefined"
The following is a strange phenomenon:
Alert (typeof age);//"Undefined"
alert (age);//Error
As above, the use of typeof still outputs "undefined" when no declaration variable is made. This requires us to initialize the variables as explicitly as possible, so that the typeof operator can be used to differentiate whether the variable is not declared or not initialized.
L NULL Type
This type also has only one value,-null, which represents an empty object pointer. Therefore, the "object" string is returned when typeof is used.
It is important to note that because undefined values are derived from null values, ECMA-262 specifies that their equality tests return true.
Example: Alert (null==undefined);//true
In any case it is not necessary to explicitly set a variable value to undefined, but for null, it is recommended to explicitly let the variable hold a null value as long as the variable intended to be saved has not actually saved the object.
L Boolean Type
This type has two literal values: True and false, which are case-sensitive.
ECMAScript specifies that all types of values can call the Transformation function Boolean () to convert itself to a corresponding Boolean value.
Conversion rules:
Data type |
True |
False |
Number |
Any non-0 numeric value |
0 and Nan |
String |
Any non-empty string |
Empty string |
Object |
Any object |
Null |
Undefined |
Not/applicable |
Undefined |
L Number Type
The most basic numeric literal format is a decimal integer
Example: Var num=44;
Octal literal the first bit must be 0, then the octet sequence (0-7)
Example: Var num1=060;//eight-binary 48
var num2=096;//invalid octal value-resolves to 96
As in num2, the values in the literal value are out of range, then the leading 0 is ignored and the subsequent values are parsed as decimal values. Note, octal literals are not valid in strict mode, causing the supported JavaScript engine to throw an error.
The first two digits of the hexadecimal value are 0x, followed by any hexadecimal digits (0-9 and a-f, where the a-f case is OK).
In arithmetic calculations, all octal and hexadecimal values are eventually converted to decimal values.
(1) floating point value
Example: Var floatnum1=1.1;var floatnum2=0.1;var floatnum3=.1;
VAT floatnum4=1.0; var floatnum5=1.;
The amount of memory space required for floating-point numbers in JavaScript is twice times that of holding integer values. As a result, ECMAScript will convert floating-point numbers into integers in some cases, such as the FLOATNUM4 and FLOATNUM5 defined above.
Scientific notation in ECMAScript: (E is an exponent in the power of a 10-bit base)
Example: Var floatnum6=3.1215e10; var floatnum7=4e-10;
Note: Because the arithmetic operations of floating-point numbers are not more accurate than integers, do not try to test floating-point arithmetic. Example: Alert ((0.1+0.2) ==0.3);
(2) Range of values
ECMAScript can be expressed in the
The maximum value is Number.min_value or number.positive_infinity, the minimum value is Number.MAX_VALUE or number.negative_infinity, Values that are out of range are automatically converted to Infinity and-infinity.
You can use the Isfinite () function to determine whether a number is between the maximum and minimum values.
(3) NaN
Used to represent a case where an operand that would have returned a numeric value did not return a number.
Any value divided by 0 returns Nan.
Nan has two characteristics, and any number involving a Nan operation will return nan; Nan and any value are unequal, including yourself.
Example: Alert (Nan==nan)//return False
Finally, the isNaN () function is provided to detect whether a value is "not a value". isNaN automatically attempts to convert a value when it is accepted to a number.
Example: Alert (IsNaN (NaN));//true
alert (IsNaN);//false
Alert (IsNaN ("ten"));//false
Alert (IsNaN ("yellow"));//true
Alert (IsNaN ("true"));//false
(4) Numeric conversion
numeric conversions provide 3 functions, number (), parseint (), parsefloat ().
The number () function is used for any data type and has the following conversion rules:
Boolean worth True and False will be converted to 1 and 0 respectively;
The numeric value is simply passed in and returned;
A null value returns 0;
Undefined returns Nan;
If the string is slightly more complicated, do not take specific notes here (P30)
Example: Var num1=number ("Hello");//nan
var num2=number ("");//0
var num3=number ("000011");//11
var num4=number (true);//1
parseint () and parsefloat () are only converted to strings. For the parseint () function, if the first character is not a numeric character or a minus sign, the return value is Nan, that is, parseint () converts the empty string to Nan, and number () returns 0 to the empty string, if the first is a numeric character, It is then parsed until the last or non-numeric characters are encountered, and this function provides the specified cardinality as the second argument to specify the parsing format. The difference between the parsefloat () function is that it resolves the first decimal point of the string to be valid, the second one is invalid, and the parsefloat () function resolves only the decimal value, so it automatically ignores the previous 0 in the string.
Example: Var num1=parseint ("1123yellow");//1123
var num2=parseint ("");//nan
var num3=parseint ("0xA");//10
var num4=parseint ("22.5");//22
var num5=parseint ("070");//56
var num6=parseint ("AF", 16);//175
var num7=parseint ("AF");//nan
var num8=parsefloat ("19yellow");//19
var num9=parsefloat ("0xA");//0
var num10=parsefloat (22.5.4);//22.5
var num11=parsefloat (000908.5);//908.5
L sring Type
ECMAScript specifies that strings can be represented by double quotation marks (") or single quotation marks (').
(1) Character literal
Also called an escape sequence, used to represent nonprinting characters or characters that have other uses. Cases:
\ nthe newline \ t tab \b Space \ r enter \ ' single quotation mark \ ' double quotation mark
\ \ Slash \unnnn Unicode characters
These character literals will be parsed as a character.
(2) String characteristics
ECMAScript the string is immutable, once created, its value does not change.
Example: Var lang= "Java";
lang=lang+ "Script";
On the second line, first create a new string that holds 10 characters, then populate the string with "Java" and "script", and finally destroy the original string "Java" and "script".
(3) Convert to String
There is a ToString () method In addition to the null and undefined values, and by passing the cardinality, you can output a string of different formats, and the decimal string is returned by default without a parameter.
Example: Var num=10;
Alert (num.tostring ());//"10"
Alert (num.tostring (2));//"1010"
alert (num.tostring);//"A"
For null and undefined type values, a string () is used to return the corresponding value.
Example: Var value1=null;
var value2;
Alert (String (value1));//"NULL"
Alert (String (value2));//"Undefined"
L Object Type
An object in ECMAScript is a collection of data and functions. Creating objects with the new operator
Example: Var o=new Object ();
Any properties and methods that the object type has also exist in more specific objects.
N Constructor: Saves the function used to create the current object
N hasOwnProperty (PropertyName): Checks whether a given property exists in the current object
n isPrototypeOf (object): Used to check if an incoming object is a prototype of another object
N propertyisenumerable (PropertyName): Checks whether the given property can be enumerated using the For-in statement.
n toLocaleString ()
n toString ()
n ValueOf ()
Note: Object behavior in ECMA-262 does not necessarily apply to other JavaScript objects, such as objects in the BOM and Dom (host objects).
JavaScript data types