1. Variables
The variables in JavaScript are loosely typed and can be used to hold any type of data.
A variable defined with the var operator becomes a local variable in the scope that defines the variable.
You can create a global variable after omitting the var operator. (Not recommended)
You can define multiple variables using a single statement:
var message = "HI", false; = 26;
2. Data type
Simple data type: Undefined, Null, Boolean, number, String.
Complex data type: Object. object is essentially a set of unordered name-value pairs.
- typeof detects the data type of a given variable, is an operator rather than a function
- Undefined contains Undefined A variable is not the same as a variable that has not been defined:
var//undefinedalert (age); // error occurred
- NULL if the defined variable is intended to be used to hold the object in the future, it is better to initialize the variable to null instead of the other value.
- Boolean This type has only two literals: true and false. Although there are only two literals, you can call a Boolean function on a value of any data type, and a Boolean value is returned.
Converted to true:
Boolean (any non-empty string)
Boolean (any non-0 numeric value)
Boolean (Any object)
Converted to false:
Boolean ("")
Boolean (0 and NaN)
Boolean (NULL)
Boolean (undifined)
Number uses an IEEE 754 64-bit double-precision floating-point number encoding.
The first digit of the octal literal must be 0, followed by the octal number sequence (0-7). If the number in the literal is more than 7, then the first 0 is ignored, and the subsequent value is parsed as a decimal value.
var // octal var // invalid octal, converted to decimal var num3 =; // invalid octal, converted to decimal 8
The first two digits of hexadecimal literals must be 0x.
In arithmetic calculations, all numeric values in eight and hexadecimal are eventually converted to decimal values.
The highest precision for floating-point numbers is 17 decimal places. 3.125e7 = 3.125 * 10^7.
4, because there is no block-level scope in ECMAScript, variables defined inside the loop can also be accessed externally.
5,with statements
The function of the WITH statement is to set the scope of the code to a specific object: with (expression) statement;
var qs = location.search.substring (1); var hostname = location.hostname; var url = location.href;
After using with:
with (location) { var qs = search.substring (1); var hostname = hostname; var url = href;}
In the above example, the location object is associated with a with statement, which means that each variable is first considered a local variable inside the code block of the WITH statement, and if the definition of the variable is not found in the local environment, the location object is queried for properties with the same name. If a property of the same name is found, the value of the Location object property is used as the value of the variable.
6, switch statement
You can use any data type in a switch statement, no matter whether it is a string or an object.
7, function
The ECMAScript function does not mind passing in the number of arguments, and does not care about what data type to pass in the parameters. The reason is that the arguments in the ECMAScript are internally represented by an array, and the function accepts that the array is the same, not the parameters contained in the array.
JavaScript Advanced Programming