grammar

Strict mode

  1. It is a preprocessor that the JavaScript engine sees and switches to strict mode;
  2. It also has scopes, such as a JS file or a function;
"use strict";
//code
function func(){
"use strict";
}
Copy the code

Automatic Semicolon Insertion

In C, a semicolon is the end of a statement and cannot be omitted. JavaScript’s semicolons are optional, though it’s probably best to get in the habit of writing semicolons. Adding semicolons can also help performance in some cases, as parsers try to fill in the semicolons in the right places to correct syntax errors.

This problem can ultimately be traced back to the workings of the JavaScript interpreter, which I won’t delve into for now.

The ASI mechanism in JavaScript allows us to omit the semicolon. The ASI mechanism does not mean that semicolons are automatically added to the code during parsing. Rather, the parsers use semicolons as a basis for sentence breaking to ensure correctness of parsing.

Three rules of ASI

  1. When an impermissible line terminator (

    ,

    ) or} occurs, a semicolon is inserted before it.

    { 1 2 } 3// will be converted by ASI to
    ​
    { 1 2; }3;
    Copy the code
  2. When the end of an identifier input stream is captured and a single input stream cannot be converted into a complete program, a semicolon is inserted at the end.

    a = b
    ++c
    ​
    // will be converted by ASI to
    ​
    a = b;
    ++c;
    Copy the code
  3. A semicolon is inserted at the end of a statement that contains a restriction item in the syntax followed by a line terminator. Statements with the “there is no line terminator here” rule are:

    • Postscript operator (++--)
    • continue
    • break
    • return
    • yield,yield*
    • module
    return
    a + b
    ​
    // will be converted by ASI toreturn;
    a + b;
    Copy the code

The operator

Increasing (++) and decreasing (–) transformation rules

  • For strings, if the character form is valid, convert to numeric and apply the change, otherwise convert the convenient value toNaN.
  • For booleans, iftrue, convert to 1 iffalse, convert to 0, and apply the change.
  • For floating-point numbers, add or subtract 1.
  • For objects, passvalueOf()Method to obtain operable values and apply the above rules to the resulting values. If it isNaNThe calltoString()And apply the above rules again.

Take the inverse operator

  • You can use!!!!!To convert a variable of any type to a Boolean value, the first exclamation mark always returns a Boolean value, and the second exclamation mark inverts that value to get the true Boolean value.

Equality and congruence

  • a==b: Equal after conversion.
  • a===b: Equal without conversion.

statements

for-in

  • for-inStatement does not guaranteeReturns the order of objectsBecause the properties of objects in ECMAScript areA disorderly.
  • The variable to be iterated over isnullorundefined, the loop body is not executed.

switch

  • switchStatements can be used toAll data types.
  • The value of the condition need not be constant.

return

  • returnReturns if an empty statement is returnedundefined.