Expressions and statements

Here is a line of assignment statements, where 1+2 is called an expression and refers to a calculation to get the return value. Wherever you expect a value in JavaScript, you can use an expression that ends with a semicolon

let a = 1+2;
Copy the code

The difference between statements and expressions:

  • Statements are used to perform an operation and generally do not return a value
  • The expression returns a value, and it must return a value

Rules for identifiers

  • The most common identifiers are variable names and, later, function names
  • JS identifiers are case sensitive
  • Identifiers are named as follows: the first character can be any Unicode letter with $and underscore _, and the second character can start with a number
  • Chinese is a legal identifier that can be used as a variable name

If the else statement

if (a === 5) {
  // The statement to execute when the condition is met
} else {
  // The statement to execute if the condition is not met
}
Copy the code

More than one if… Else statements can be joined together. The else block is always paired with the if statement closest to it

if (m === 0) {
  // ...
} else if (m === 1) {
  // ...
} else if (m === 2) {
  // ...
} else {
  // ...
}
Copy the code

While for statement

The While statement consists of a loop condition and a block of code that executes over and over again as long as the condition is true

let i = 0;

while (i < 100) {
  console.log('I is currently:' + i);
  i = i + 1;
}
Copy the code

The code above loops 100 times until I equals 100

Break statement and continue statement

Both the break statement and the continue statement jump, allowing code to execute out of the existing order

  • The break statement is used to break out of a code block or loop
let i = 0;

while(i < 100) {
  console.log('I is currently:' + i);
  i++;
  if (i === 10) break;
}
Copy the code

The code above only executes the loop 10 times, and breaks out once I is equal to 10

  • The continue statement is used to immediately terminate the loop and return to the head of the loop structure to start the next loop
var i = 0;

while (i < 100){
  i++;
  if (i % 2= = =0) continue;
  console.log('I is currently:' + i);
}
Copy the code

The code above only prints the value of I if I is odd. If I is even, it goes straight to the next cycle

  • If multiple loops exist, both the break and continue statements with no arguments are for the innermost loop only

label

The statement is preceded by a label, which acts as a locator and is used to jump to any point in the program, as shown in the following example:

top:
  for (let i = 0; i < 3; i++){
    for (let j = 0; j < 3; j++){
      if (i === 1 && j === 1) break top;
      console.log('i=' + i + ', j='+ j); }}// i=0, j=0
// i=0, j=1
// i=0, j=2
// i=1, j=0
Copy the code

The above code is a double loop block, followed by the break command with the top tag (note that top is not quoted), when the condition is met, directly out of the double loop. If the break statement is not followed by a label, you can only break out of the inner loop and enter the next outer loop

Tags can also be used to jump out of code blocks:

foo: {
  console.log(1);
  break foo;
  console.log('This line will not print');
}
console.log(2);
/ / 1
/ / 2
Copy the code

The above code executes to break foo and jumps out of the block