Expressions and statements

expression

The value of the 1+2 expression is 3. The value of the add(1,2) expression is the return value of the function. The value of the console.log expression is the function itself. The diagram below:

The function itself f log(){[native code]}.console. log(3) prints 3, with the value undefined.

statements

Let a = 1 is a statement

The difference between the two

Expressions generally have values; statements may or may not have values. Statements generally change the environment (declaration, assignment) and the above two statements are not absolute.


Case discrimination

  • Don’t confuse

Var a is different from var A. Object is different from object. Function is different from function

The blank space

  • Most of the space is meaningless

Var a=1; var a=1; var a=1;

Undefined is automatically added after return. This is original and not good.

identifier

  • The rules

The first character can be a Unicode letter or $or _ or Chinese. The following characters, in addition to the above characters, can also be followed by numbers.

  • Variable names are identifiers
Var $= 2 var ______ = 6 var hello = 'hello'Copy the code
  • Other identifiers use MDN

annotation

Classification of annotations

  • Bad comment

Translate code into outdated comments in Chinese

  • Good comment

Experience comments for trampling holes explain comments that encounter bugs

Block block

  • Package your code together
{
    let a = 1
    let b = 1
}
Copy the code
  • Often withif / for / whileUsed together

If statement

  • grammar

If (expression){statement 1}else{statement 2} {} can be omitted if the statement has only one sentence, but it is not recommended

  • Abnormal situation

Expressions can be very perverted, like a = 1 statement 1 can be very perverted, like nested if else statement 1 can be very perverted, like nested if else indentation, like interview questions

A = 1 if(a === 2) console.log('a') console.log('a = 2')Copy the code

The second line is a statement other than the if statement, printing ‘a equals 2’, the value of which is undefined. So the result is shown above.

So use the most unambiguous way to write it (programmer commandment)

The most recommended way of writing

If (expression){statement}else if(expression){statement}else{statement}Copy the code

This is recommended

Function fn(){if(expression){return expression} if(expression){return expression} return expression}Copy the code

A switch statement

  • grammar
switch(fruit){
    case "banana":
        // ...
        break;
    case "apple":
        // ...
        break;
    default:
        // ...
}
Copy the code
  • break

Most of the time, you can’t omit the break. Operation as shown below:


Question mark colon expression

  • The expression 1? Expression 2: Expression 3

Replace if/else statements to simplify use when there is only one if statement and one else statement.


&& short circuit logic

  • A && B && C && DTake the first false valueAorD, does not take true/false

| | short circuit logic

  • A|| B || C || DTake the first truth valueAorD, does not take true/false

The principle is similar to the diagram above.


The while loop

  • grammar

While (expression){statement} Check whether the expression is true or false. If the expression is true, execute the statement. If the expression is false, execute the following statement

  • other

do… While is used incorrectly, you can check the documentation to understand

The for loop

  • Syntactic sugar

For is a convenient way to write the while loop where is it convenient?

  • grammar
For (statements 1; Expression is 2; Statement 3){body of loop}Copy the code

Execute statement 1(only once) and check if expression 2 is true, execute the body of the loop. Then check if expression 2 is false, exit the loop and execute the following statements.

breakandcontinue

  • Exit current {} all loops V.S. exits the current loop

Label statement

Rarely used, often used in interview questions

  • grammar
foo:{ console.log(1); break foo; Console. log(' this line will not output '); } console.log(2);Copy the code

  • The interview questions
{
    foo: 1;
}
Copy the code



It’s a block of code with a tag foo that says 1. Not objects.


Recommended books

Ruan yifeng’s free tutorial: Getting started with JavaScript you don’t know