1. What are expressions and statements

expression

  • 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

statements

  • Var a = 1 is a statement

The difference between expressions and statements

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

2. Rules for identifiers

  • The first character can be a letter, $, or Chinese
  • The following characters can also be numbers
  • For example,
var a = 1; var $ = 2; Var hello = 3; var __ = 6; var $4 =5Copy the code

3. If the else statement

Grammar:

If (expression) {statement 1} else {statement 2}

  • For example,
if ( a>b ) { 
    do sth 
}else{
    do sth 
    }
Copy the code
  • ? Method of:

a > b ? A: b Indicates that a is returned if a> B, otherwise B is returned

Conditional statement summary

  • if… else…
  • Switch: Remember to write break
  • A? B:C
  • A&&B
  • a||b,a=a||b

4. While for statement

while

When · · · · · ·

  • Syntax: while (expression) {statement}
  1. Determine whether the expression is true or false
  2. When the expression is true, the statement is executed and the expression is true or false after execution
  3. When the expression is false, the following statement is executed
  • For example,
var a = 1 while (a ! == 10){ console.log(a) a = a + 1 }Copy the code

for

  • For is a convenient way of writing while
  • Syntax: for (statement 1; Expression 2; Statement 3) {body of loop}
  1. Execute statement 1, and then determine expression 2,
  2. If true, the body of the loop is executed first, followed by statement 3
  3. If false, exit the loop and execute the following statement
  • For example,
for (var a=1; a ! = = 10; a++){ console.log(a) }Copy the code

5.break continue

break

During the loop, you can use the break statement to break out of the current loop. Let’s look at an example:

for(var i=0; i < 10; i++){
    if(i%2===1){
        break
        }
    }
Copy the code

When I is singular 1, it breaks out of the loop

continue

Continue Ends this loop early and continues the next loop. Let’s look at an example:

for(var i=0; i < 10; i++){
    if(i%2===1){
        continue
        }else{
            console.log(i)
            }
    }
Copy the code

If I is odd, skip it and continue the loop. If I is even, print the value of I

label

The JavaScript language allows statements to be preceded by a label, which acts as a locator to jump to any point in the program, in the following format.

Label: statementCopy the code

The tag can be any identifier, but cannot be a reserved word, and the statement part can be any statement.

Tags are often used with break and continue statements to break out of a particular loop.

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