Expressions and statements

The 1+2 expression is 3. The add(1,2) expression is the return value of the function console.log expression is the function itself console.log(2) expression is undefined. Var a = 1 is a statement. Statements may or may not change the environment (declaration, assignment)

Space Precautions

Spaces and carriage returns don’t really matter in most cases, but you can’t follow a return by carriage return, which returns undefined

identifier

Rule: The first character can be a Unicode letter or $or _ or Chinese, and the following character, in addition to the above, can have a number.

If statement

If (expression){} {} can be omitted if there is only one statement

A Switch statement

A break is written for each case, and if there is no break, the following cases are executed in sequence until a break is encountered

&& short circuit

A &&b: returns B if a is true, and directly returns A if a is false, short-circuit B a&&b&&c&& D: returns the first false value or returns D

| | short circuit

A | | b: if a is false, return to b, if a is true, returns a, short circuit b common application scenario set default values for a variable a = a | | 100 if a is a = a, if a does not exist, a = 100

The while loop

let a = 0.1;
while(a ! = =1) {
    console.log(a)
    a = a + 0.1
}
Copy the code

The code above loops indefinitely due to imprecise floating-point calculations

The for loop

for (var i = 0; i < 5; i++) {
    console.log(i)
}
console.log(i)
Copy the code

Print 5, I =4, execute I ++, exit the for loop, print 5

for (let i = 0; i < 5; i++) {
    console.log(I)} What is the I printed here?console.log(i)
Copy the code

If I is undefined, the variable declared by let will not be promoted

for (var i = 0; i < 5; i++) {
    setTimeout(() = >{
        console.log(i)
    }, 0)}Copy the code

The result is printed five times. 5 Each time the for loop is executed, and 5 is printed at the end. SetTimeout waits until the for loop is complete

for (let i = 0; i < 5; i++) {
    setTimeout(() = >{
        console.log(i)
    }, 0)}Copy the code

Replace var with let to print 0,1,2,3,4

Break and continue

Break is breaking out of the nearest for loop and continue is breaking out of the loop.

label

{
  a:1;
}
Copy the code

The above code is not an object, indicating that there is an A tag in the code block with a value of 1