Expressions and statements

  • The 1+2 expression has the value 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
  • What is the value of the console.log(3) expression? undefined

statements

Var a = 1 is a statement

The difference between the two

  • Expressions generally have values; statements may or may not
  • Statements typically change the environment (declaration, assignment)

Case sensitivity

  • Var a and var a are different
  • Object and object are different
  • Function and function are different

identifier

The rules

  • The first character can be a Unicode letter or $or _ or Chinese

The variable name

  • var _ = 1
  • var $ = 2
  • var ______ = 6
  • Var Hello = ‘hi’

Block block

{
    let a = 1
    let b = 2
}   
Copy the code
  • Often used with if/for/while

If statement

grammar

  • If (expression){statement 1}else{statement 2}
  • {} can be omitted in one-sentence statements, but this is not recommended

Recommend writing

  • The most recommended
If (expression) {statement} else if (expression) {statement} else {statement}Copy the code
  • Time to recommend
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:
        ...
        break;
}
Copy the code

Question mark colon expression

  • The expression 1? Expression 2: Expression 3

&& short circuit logic

  • A && B && C && D takes the first false value or D
  • It doesn’t take true/false
  • The commonly usedconsole && console.log && console.log('hi')

| | short circuit logic

  • C | A | B | | | | D take the first true value or D
  • It doesn’t take true/false

The while loop

grammar

  • While (expression) {statement}
  • Determine whether the expression is true or false
  • When the expression is true, the statement is executed
  • When the expression is false, the following statement is executed
  • Verify the expression again after execution

other

  • do… while

The for loop

Syntactic sugar

  • For is a convenient way to write the while loop

grammar

For (statements 1; Expression is 2; Statement 3){body of loop}Copy the code
  • Statement 1 is executed first
  • And then determine expression 2
  • If true, the body of the loop is executed, followed by statement 3
  • If false, exit the loop and execute the following statement

Break and continue

  • Exits all loops and exits the current loop

Label statement

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