First, the grammar
1. Case-sensitive
Everything in ECMAScript (variables, function names, and operators) is case-sensitive.
2. Identifiers
The so-called identifier refers to the name of a variable, function, property, or parameter of a function.
Identifiers can be one or more characters that are combined according to the following formatting rules:
- The first character must be a letter, an underscore (_), or a dollar sign ($);
- Other characters can be letters, underscores, dollar signs, or numbers.
By convention, the ECMAScript identifier is in hump-case format, that is, the first letter lowercase, and the first letter of each of the remaining words is capitalized, for example:
Firstsecond MyCar Dosomethingimportant
You cannot use keywords, reserved words, true, false, and NULL as identifiers.
3. Comments
Single-line Comment
Block-level comments begin with a slash and an asterisk (/*), ending with an asterisk and a slash (*/), as follows:
/*
* This is a multi-line
* (Block level) comments
*/
4. Strict mode
ECMAScript 5 introduces the concept of strict mode (strict modes). Strict mode defines a different parsing and execution model for JavaScript. In strict mode, some of the indeterminate behavior in ECMAScript 3 will be handled, and some unsafe operations will throw an error. To enable strict mode throughout the script, you can add the following code at the top:
"Use strict";
This line of code looks like a string and is not assigned to any variable, but it is actually a compilation instruction (pragma) that tells the supported JavaScript engine to switch to strict mode.
Include this compilation instruction above the function, or you can specify that the function is executed in strict mode:
function dosomething () {
"Use strict";
function body
}
Browsers that support strict mode include ie10+, Firefox 4+, Safari 5.1+, Opera 12+, and Chrome.
5. Statements
The statement in ECMAScript ends with a semicolon, or if the semicolon is omitted, the end of the statement is determined by the parser
You can use C-style syntax to combine multiple statements into a block of code, that is, the code block begins with a left curly brace ({) and ends with a right curly brace (}):
if (test) {
Test = false;
alert (test);
}
Ii. keywords and reserved words
ECMA-262 describes a set of keywords that can be used to represent the start or end of a control statement, or to perform a specific operation. By rule, keywords are also language-reserved and cannot be used as identifiers.
Here are all the keywords for ECMAScript (the 5th version of the New keyword with *):
Break do instanceof typeof
Case Else New Var
Catch finally return void
Continue for switch while
debugger* function This
With default if throw
Delete in try
ECMA-262 also describes another set of reserved words that cannot be used as identifiers: The following are all reserved words defined by ECMA-262 version 3rd:
abstract enum int Short
Boolean Export interface static
BYTE extends Long super
Char Final native synchronized
Class Float Package throws
Const GOTO Private Transient
Debugger implements protected volatile
Double Import Public
The 5th edition reduces the reserved words when running in non-strict mode to the following:
class Enum extends Super
Const Export Import
In strict mode, version 5th also imposes restrictions on the following reserved words:
Implements package public
Interface private static
Let protected yield
In addition to the reserved words and keywords listed above, ECMA-262 version 5th also imposes restrictions on eval and arguments. In strict mode, these two names cannot be used as identifiers or property names, or an error is thrown.
Third, variable
The ECMAScript variables are loosely typed, and so-called loose types can be used to hold any type of data.
To define a variable, use the var operator (Note that var is a keyword) followed by the variable name (that is, an identifier), as follows:
VAR message;
This line of code defines a variable named message that can be used to hold any value (such as an uninitialized variable, which holds a special value--undefined
ECMAScript also supports direct initialization of variables, so you can set the value of a variable while defining it, as follows:
var message = "HI";
A variable defined with the var operator becomes a local variable in the scope that defines the variable. That is, if you use var to define a variable in a function, the variable is destroyed after the function exits, for example:
function Test () {
var message = "HI"; Local variables
}
Test ();
alert (message); Error!
When a function is called, the variable is created and assigned a value. After that, the variable is destroyed immediately.
However, you can omit the var operator as follows to create a global variable:
function Test () {
message = "HI"; Global variables
}
Test ();
alert (message); "Hi"
Thus, once the test () function is called, the variable is defined and can be accessed anywhere outside the function.
Define multiple variables at the same time:
You can use a single statement to define multiple variables, as long as each variable (either initialized or uninitialized) is separated by commas as follows:
var message = "Hi",
Found = False,
Age = 29;
Iv. Types of data
There are 5 simple data types (also known as base data types) in ECMAScript: Undefined, Null, Bollean, number, and String.
There is also a complex data type--object,object essentially consists of a set of unordered name-value pairs.
1. typeof operator
The typeof is used to detect the data type of a given variable.
Using the typeof 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 the value is a string;
- "Number"--if the value is numeric;
- "Object"--if this value is an object or null;
- "Function"--if this value is a function.
Calling typeof Null will return "object" because the special value NULL is considered an empty object reference
2. Undefined type
The Undefined type has only one value, that is, a special Undefined. When declaring a variable with VAR but not initializing it, the value of this variable is undefined
VAR message;
Alert (message = = undefined); True
Alert (Message = = = undefined); True
VAR message; The undefined value is obtained by default after this variable declaration
The following variable does not declare
var age
alert (message); "Undefined"
alert (age); Error generated
Alert (typeof message); "Undefined"
Alert (typeof age); "Undefined"
The typeof operator, which executes the uninitialized and undeclared variables, returns the undefined value, and the result is logically justified. Because although these two variables are fundamentally different from the technical point of view, it is virtually impossible to perform real operations on any of these variables.
3. Null type
The null type is the second data type with only one value, and this special value is null.
From a logical point of view, a null value represents an empty object pointer, which is why the "object" is returned when using the TypeOf operator to detect null values
var car = null;
Alert (typeof car); "Object"
If a defined variable is intended to be used to hold an object in the future, it is better to initialize the variable to null instead of to another value.
In fact, the undefined value is derived from a null value, so ECMA-262 specifies that the equality test for them returns true:
Alert (Null = = undefined); True
4. Boolean type
The Boolean type is the most used type in ECMAScript, which has only two literals: true and false.
The literal value of Boolean literals True and false is case-sensitive.
Although there are only two literals of the Boolean type, the values of all types in ECMAScript have values equivalent to these two Boolean values. To convert a value to its corresponding Boolean value, you can call the Transform function Boolean (), for example:
var str1= "HI";
var strboolean=boolean (STR1);
Console.log (Strboolean);//true
You can call the Boolean () function on a value of any data type, and always return a Boolean value.
Various data types and their corresponding conversion rules:
Data type |
The value converted to true |
Value converted to False |
Boolean |
True |
False |
String |
Any non-empty string |
"" (empty string) |
Number |
Any non-0 numeric value (including infinity) |
0 and Nan |
Object |
Any object |
Null |
Undefined |
|
Undefined |
A flow control statement, such as an if statement, automatically performs a corresponding Boolean conversion.
var message = "Hello world!";
if (message) {
Alert ("Value is true");
}
5. Type of number
This type uses the IEEE754 format to represent integers and floating point values (floating-point numbers are also known as double-precision values in some languages). To support various numeric types, ECMA-262 defines different numeric literal formats.
The most basic numeric literal format is a decimal integer, in addition to decimal notation, integers can be represented by octal (base 8) or hexadecimal (base 16) literals. Where the first digit of the octal literal must be 0 (0), followed by the octal number sequence (0~7).
var octalNum1 = 070; Eight-binary 56
var octalNum2 = 079; Invalid octal value--resolves to 79
var octalNum3 = 08; Invalid octal value--resolves to 8
Octal literals are not valid in strict mode, causing the supported JavaScript engine to throw an error.
The first two digits of the hexadecimal literal must be 0x, followed by any hexadecimal digits (0~9 and a~f). Where the letter a~f can be uppercase or lowercase. As shown in the following example:
var hexNum1 = 0xA; Hex of 10
var hexNum2 = 0x1f; Hex of 31
In arithmetic calculations, all values in eight and hexadecimal are eventually converted to decimal values.
(1) floating point value
The so-called floating-point value is that the number must contain a decimal point, and must have at least one digit after the decimal.
var floatNum1 = 1.1;
var floatNum2 = 0.1;
var floatNum3 =. 1; Valid, but not recommended
Because the memory space required to hold a floating-point value is twice times the value of the saved integer, ECMAScript will lose no chance in converting the floating-point value to an integer value. Obviously, if the decimal point is not followed by any number, then this value can be saved as an integer value. Similarly, if the floating-point value itself represents an integer (such as 1.0), the value is also converted to an integer, as shown in the following example:
var floatNum1 = 1.; No digits after the decimal point--resolves to 1
var floatNum2 = 10.0; integer--resolves to 10
For those large or small values, a floating-point numeric representation can be expressed in e notation (i.e. scientific notation). The value represented by the E notation equals the number in front of e multiplied by the exponential power of 10. The same is true of the format of e notation in ECMAScript, which is preceded by a numeric value (which can be an integer or a floating-point number), an uppercase or lowercase letter E, followed by an exponent in the power of 10, which is used to multiply the preceding number. Here is an example of a numeric value using e notation:
var floatnum = 3.125e7; equals 31250000
Here, the actual meaning of E notation is "3.125 times 10 of 7".
You can also use the E notation to represent very small values, such as 0.00000000000000003, which can be represented by a more concise 3e-17. By default, Ecmasctipt converts floating-point numbers with 6 or more 0 after the decimal point to numeric values represented in e notation (for example, 0.0000003 is converted to 3e-7).
The highest precision of a floating-point value is 17 decimal places, but it is far less accurate than an integer in arithmetic calculations. For example, 0.1 plus 0.2
The result 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) {//Do not do such a test!
Alert ("You got 0.3.");
}
Console.log (0.1+0.2);//0.30000000000000004
Console.log (0.2+0.7);//0.8999999999999999
Console.log (0.1+0.7);//0.7999999999999999
Console.log (0.1+0.1);//0.2
Console.log (0.2+0.2);//0.4
Console.log (0.1+0.3);//0.4
(2) Range of values
The minimum value that ECMAScript can represent is saved in Number.min_value--in most browsers, the value is 5e-324; The maximum value that can be represented is saved in Number.MAX_VALUE--in most browsers, This value is 1.7976931348623157e+308.
If the result of a calculation results in a value that exceeds the range of JavaScript values, the value is automatically converted to a special infinity value. Specifically, if the value is negative, it is converted to-infinity (negative infinity), and if the value is positive, it is converted to Infinity (positive infinity).
As mentioned above, if a calculation returns a positive or negative infinity value, the value will not continue to participate in the next calculation because infinity is not able to participate in the calculated value. To determine whether a value is poor (in other words, between the minimum and maximum values), you can use the Isfinite () function. This function returns True when the parameter is between the minimum and maximum values, as shown in the following example:
var result = Number.MAX_VALUE + Number.MAX_VALUE;
Alert (isfinite (result)); False
Although certain values are rarely exceeded in calculations, it is possible and necessary to monitor these values when performing calculations of very small or large numbers.
Access to number.negative_infinity and number.positive_infinity can also get negative and positive INFINITY values. As you can see, these two properties are saved with-infinity and Infinity respectively.
Console.log (Number.MAX_VALUE);//1.7976931348623157e+308
Console.log (number.min_value);//5e-324
(3) NaN
NaN, that is, a non-numeric (not a number) is a special value that represents a case where an operand that would have returned a numeric value does not return a number (so that no error is thrown). In ECMAScript, any number divided by 0 will return Nan.
The NaN itself has two unusual features. First, any operation involving Nan, such as NAN/10, will return Nan, a feature that can cause problems in a multi-step calculation. Second, Nan is not equal to any value, including the Nan itself. For example, the following code returns false:
Alert (nan = = Nan); False
For these two characteristics of Nan, ECMAScript defines the isNaN () function. This function takes a parameter, which can be any type, and the function will help us determine whether the parameter is "not a value". IsNaN () after receiving a value, it attempts to convert the value to a number. Some values that are not numeric are converted directly to numbers.
Any value that cannot be converted to a number will cause the function to return true.
Alert (IsNaN (NaN)); True
Alert (IsNaN (10)); False (10 is a numeric value)
Alert (IsNaN ("10")); False (can be converted to a value of 10)
Alert (IsNaN ("Blue")); True (cannot be converted to numeric value)
Alert (IsNaN (true)); False (can be converted to a value of 1)
The basic concept of the third chapter-"JavaScript Advanced Programming"