1. Expressions

An expression is a collection of code that returns a value.

For example, the expression a=7. This expression uses the = operator to assign the value 7 to the variable x, which itself has a value of 7.

Expression: 3+4. This expression uses the + operator to add 3 and 4 together but does not assign the result (7) to a variable.

2, statements,

var a=1; Is a statement

A computer program is a series of “instructions” that are “executed” by a computer. In a programming language, these programming instructions are called statements. A JavaScript program is a series of programming statements.

Terminating a statement with a semicolon is not required and is usually recommended. Allow multiple statements on the same line if separated by semicolons:

a = 5; b = 6; c = a + b;
Copy the code

3. Naming rules for JS identifiers

In JavaScript, the naming conventions for identifiers are the same as those in Java and many other languages. The main conventions are as follows:

1. The first character of an identifier must be a letter, underscore (_) or dollar sign ($) or Chinese characters, and the following characters can be letters, digits, or underscores (_), dollar sign, or Chinese characters.

2. User-defined identifiers cannot have the same name as keywords or reserved words in JavaScript, but can contain keywords or reserved words.

3. Identifiers cannot contain Spaces.

4. The identifier cannot contain special characters such as +, -, @, and #.

5. There are two main ways to name compound identifiers consisting of multiple words:

Use an underscore to connect each word in lower case, for example, use_name.

② Is the use of hump, which is divided into large hump and small hump. The format of the big hump is to capitalize the first letter of each word and lower the rest of the letters, for example: UseName; The format of the little hump is all lowercase for the first word, uppercase for each word at the beginning of the second word, and lowercase for the rest of the letters, such as useName.

Examples of valid identifiers:

User_name userName _name $name ab ab123 userNameCopy the code

Examples of invalid identifiers:

1a     // The first character is a number
a b    // The identifier contains Spaces
a@b    // The identifier contains special symbols
while  / / key
Copy the code

4, if… else…

In the if()else structure, statement 1 in the program is executed when a particular condition is true, that is, when the expression is true today; Statement 2 is executed only if the expression is false.

if(expression){statement1 
}else{statement2
}
Copy the code

The if() and else constructs can be further extended with else if() :

if(expression){statement1 
}else if{statement2
}else{statement3
}
Copy the code

If the code block has only one statement, there is no need to put the code block in curly braces. Without curly braces, the code still runs correctly. However, without curly braces, the reader of the code may not know what it means, so it is best to include them.

5. While loop

The while statement is equivalent to the repeated if(). Provides an expression for while() that executes a block of code if the result of the expression is true. Unlike if(), while() checks that expression after each block of code is executed. If the block is not actually executed again; When the expression is false, the statement following the code block is executed. Example:

var i=1;
while(i<=5)} {console.log(i);
  i++;
}
Copy the code

The print result is as follows:

1
2
3
4
5
Copy the code

6. For loop

The for() structure can also execute the same statement multiple times.

Example:

for(var i=1; i<=5; i++){console.log(i);
}
Copy the code

The print result is as follows:

1
2
3
4
5
Copy the code

For () is a little more complicated than while(). Instead of a single test expression inside the parentheses, there are three expressions separated by good separation: the initialization expression, the test expression, and the iteration expression. The initialization and iteration expressions of the for() structure are more concise.

The execution process needs to be noted as follows:

1. Initialize the expression first. Initialize the expression only once.

2. Then execute the test expression if the result is true. Execution loop body;

3. Run the iteration expression after executing the loop body;

4. Execute the test expression again to determine whether the result is true. If true, continue with the loop body; otherwise, execute the statement after for().

7. Break and Continue

The break statement “breaks” the loop.

The continue statement “skips” an iteration in the loop.

Break statement

The break statement can also be used to break out of a loop.

The break statement breaks the loop and continues the code that follows the loop (if any)

The break statement breaks the nearest nested loop in multiple loops

Break the sample:

for(var i=1; i<=5; i++){if (i === 3) { break; }
  console.log(i);
}
Copy the code
The continue statement

The continue statement interrupts an iteration (in a loop) if the specified condition occurs. Then proceed to the next iteration in the loop.

Example skip value 3:

for(var i=1; i<=5; i++){if (i === 3) { continue; }
  console.log(i);
}
Copy the code

8, the label

Mark the statement

A tag is a statement preceded by an identifier that can be referenced.

Common usage scenarios are as follows:

You can use a label to uniquely mark a loop, and then use a break or continue statement to indicate whether the program breaks the loop or continues execution.

Example: Use a labeled continue statement in a for loop

var i, j;

    loop1:
    for (i = 0; i < 3; i++) {      // The first for statement is marked as "loop1"
       loop2:
       for (j = 0; j < 3; j++) {   // The second for statement is marked "loop2"
          if (i === 1 && j === 1) {
             continue loop1;
          }
          console.log('i = ' + i + ', j = '+ j); }}// Output is:
// "i = 0, j = 0"
// "i = 0, j = 1"
// "i = 0, j = 2"
// "i = 1, j = 0"
// "i = 2, j = 0"
// "i = 2, j = 1"
// "i = 2, j = 2"
Copy the code

Note: In strict mode, you cannot use “let” as the tag name. It throws a SyntaxError (because let is a reserved identifier).