Expressions and statements

JavaScript is executed in lines, which means that a line is executed in a single line, usually a single statement. A statement is an operation performed to complete a task. For example, var a = 1 + 2. This is an assignment statement that uses the var command to declare variable A and assign the result of 1+2 to variable A. Expression: 1+2 is called an expression and refers to an expression that evaluates to a return value. The difference between a statement and an expression is that a statement performs an operation and generally does not require a return value. The expression must return a value in order to return a value.

identifier

An identifier is a legal name used to identify various values. The most common identifiers are variable names. Naming rules:

  • The first character can be any Unicode letter, as well$And the underline_
  • The second letter and the following characters, in addition to the above three, can also use the numbers 0-9.
  • Chinese is a legal identifier that can be used as a variable name
  • JavaScript has some reserved words, cannot be used as identifiers: the arguments, for example, brak, case, catch, const, continue, the default, delete, do, else, eval, etc.

Conditional statements

If statement

The if structure determines the Boolean value of an expression and then executes different statements based on whether the Boolean value is true or false. if.. Else structure, if code block can be followed by an else code block, code to execute if the condition is not met. More than one if… Else statements can be written together.

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

The else block is always paired with the if statement closest to it

A switch statement

More than the if… You can use the switch structure when else is linked together. You can select the corresponding case according to the value of the variable. If all the cases do not match, the final default part is executed. Note that each case block must have as many breaks as possible, otherwise the next case block will be executed instead of jumping out of the switch structure.

Ternary operator

JavaScript ternary operators: (conditional)? Var even = (n % 2 === 0)? Var even = (n % 2 === 0)? true : false; If n is divisible by 2, then even equals true, otherwise equals false, as in the following code.

var even;
if ( n % === 0) {
even = true;
} else {
even = false;
}
Copy the code

This ternary operator can be thought of as if… Else short form.

The while loop

The while statement consists of a loop condition and a piece of code that executes over and over again as long as the condition is true. An example of a while statement:

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

This code will loop 100 times until I equals 100.

The for loop

The for statement is another form of the loop command that specifies the start, end, and termination conditions for the loop. There are three expressions in parentheses after the for statement:

  • Initialize expression: Determines the initial value of a loop variable and executes it only once at the start of the loop.
  • Conditional expression (test): This expression is executed at the start of each loop, and the loop continues only if the value is true.
  • Increment: The last operation of a loop, usually used to increment a loop variable.

Ex. :

var x = 3;
for(var i = 0; i < x; i++) {
console.log(i);
}
Copy the code

All for loops can be rewritten as while loops. Any or all of the three parts of the for statement can be omitted.

Break and continue statements

The break statement is used to break out of a code block or loop

var i = 0; While (I < 100) {console.log(' I currently: '+ I); i++; if (i === 10) break; // The code executes the loop 10 times. When I = 10, the loop will break out.Copy the code

The tag label

Statements are preceded by labels that act as locators to jump to any point in the program. Tags are often used in conjunction with break and continue statements to break out of a particular loop.