Re-consolidate JS series, are more basic things, can be checked to fill gaps, quickly read, this is the third. Other JS reconsolidation series:

  • Re-consolidate JS (I) — JavaScript primitive types
  • Re-consolidate JS (2) – JavaScript operators and expressions

1. Conditional statements

1.1 If else statements

If the condition is true, statement 1 is executed; if the condition is false, statement 2 is executed. The else part can be omitted
if(Condition) {statement1 
} else{statement2 
}

/ / example:
let score = 75
if ( score > 80) {
    console.log('A')}else if ( score > 60) {
    console.log('B')}else {
    console.log('C')}// score = 75, < 80 but > 60
Copy the code

1.2 switch statement

// If the expression is equal to 1, execute statement 1. If a break statement is encountered, the switch statement is broken. If no match is found, execute statement in default.
switch(expression) {case1Statements:1
        break
    case2Statements:2
        break
    defaultStatements:3
        break	
}

/ / case
let score = 'B'
switch ( score ) {
    case 'A':
        console.log('> 80')
        break
    case 'B':
        console.log('60 ~ 80')
        break
    default :
        console.log('< 60')}// score matches case 'B', so output: 60 ~ 80
Copy the code

Note: Normally, we use a break statement after each case statement to break out of the switch statement. If not, all matching case statements are executed until a break is encountered.

2. Loop statements

2.1 while statement

When representation reach is true, statement 1 is executed until the expression is false.
while(expression) {statement1
}
Copy the code

Note: In statement 1, the expression must be operated on so that it becomes false within a certain number of loops, otherwise an infinite loop will occur.

2.2 Do while statement

// Execute statement 1 first and then decide whether to continue with statement 1 based on whether the expression is true or false
do{statement1
} while(Expression)Copy the code

Note: In any case, the do while statement always executes statement 1 first.

2.3 for statement

// Execute the initialization part first, then check the loop condition, if true, execute statement 1, and finally execute the update expression part. If false, the for loop ends.
forInitialization; Cyclic conditions; Update expression) {statement1
}
Copy the code

Note: Neither the initialization nor the update expression part is required. The initialization part can be placed outside the for statement, while the update expression part can be placed inside statement 1.

2.4 For in statement

Used to traverse properties of an object.

// Iterate over the properties of the object
for(the property nameinObject) {statement1
}
Copy the code

Note that the for in statement also iterates over properties on the prototype chain (__proto__, constructor). If you only want to iterate over properties of the object itself, use the following methods:

for ( letThe property nameinObject) {if(object. HasOwnProperty) {statement1}}Copy the code

3. Control statements

3.1 the continue statement

The continue statement is used in a loop to skip the current loop and go to the next loop.

3.2 break statement

The break statement is used to break out of a loop or switch statement.

4. Exception capture statement

If statement 1 fails to execute, it is caught by a catch statement. Statement 2 is followed by statement 3 in the finally block. Statement 3 in the finally statement block is executed regardless of whether statement 1 has an exception. * /
try{statement1
} catch(exception) {statement2
} finally{statement3
}

/ / case
try {
    console.log(myName)
} catch ( exception ) {
    alert( exception)
} finally {
    alert('bad dream! ')}/** * In the try block, we tried to print the value of the variable myName, but the variable myName was not defined, so the exception was caught by the catch block, and the popup said: ReferenceError: MyName is not defined, * Finally statement, popup: bad dream! * /
Copy the code

5. The with statement (not recommended)

Allows us to access object properties more briefly. The with statement is a performance issue and can cause unnecessary confusion in scope. It is also disabled in ES5 strict mode and is not recommended.

with(expression) {statement}/ / example:
let cc = {
    name: 'cc'.age: 100.company: {
        name: 'BEST'.address: 'Hang Zhou'}}with(cc) {
    console.log(`company name:${company.name}, company address: ${company.address}`)}Company name:BEST, company address: Hang Zhou

// Alternative methods:
let company = cc.company
console.log(`company name:${company.name}, company address: ${company.address}`)
Company name:BEST, company address: Hang Zhou
Copy the code