1. Expressions and statements

A statement is an operation that is performed to complete a task. For example, here is a line of assignment.

var a = 1 + 3;
Copy the code

1+3 is an expression and var a = 1+3 is a statement. Expressions and statements cannot be completely distinguished. In general, expressions have values; statements may or may not have values. Statements typically change declarations and assignments.

;;;
Copy the code

A semicolon can be preceded by nothing, and the JS engine treats it as an empty statement.

Functions are expressions, and only functions return values.

console.log(3)
3
undefined
Copy the code

The return value of the console.log expression is the log function itself

Console. log(3) returns undefined, not 3, which is just a printed value.

2. The identifier

An identifier is a legal name used to identify various values.

The most common identifiers are variable names and, later, function names. Identifiers in JS are case sensitive. For example: A is different from a, var a is different from var a, object is different from object.

Naming rules for identifiers

  • The first character, which can be any Unicode letter (including English and other languages), and the dollar sign ($) and underline (_).
  • In addition to Unicode letters, dollar signs, and underscores, digits can be used for the second and subsequent characters0-9.

Example: Invalid identifier

1A // The first character cannot be a digit 23 // same as the preceding *** // The identifier cannot contain the asterisk a+ B // The identifier cannot contain the plus sign -d // The identifier cannot contain the minus sign or the conjunction lineCopy the code

Chinese is a legal identifier that can be used as a variable name.

Var temporary variable = 1;Copy the code

3. Comments

The part that the JS engine ignores is called a comment, and its purpose is to explain the code.

  • Single-line comment, with//At the beginning.
  • Multi-line comments, placed in/ *and* /In between.
// This is a single line comment /* this is a multi-line comment */Copy the code

4. Block

JS uses curly braces to group multiple related statements together, called blocks.

{
  let a = 1;
  let b = 2;
}
Copy the code

5. Conditional statements

If statement

  • Syntax: if(expression) {statement 1} else {statement 2}
  • If the expression value is true, statement 1 is executed, otherwise statement 2 is executed.
  • {} can be omitted in one-sentence statements, but this is not recommended

Nested if in special case 1 statement 1

var a = 2; If (a<100) if(a<10) console.log('a less than 10') =>a less than 10Copy the code

Nested if in special case 2 statement 2

var a = 2; If (a==1){'a<1'} else if(a<100){console.log('a less than 100')}else console.log(' less than 100') => A less than 100Copy the code
var a = 2; If (a = = 1) {} 'a < 1' else if (a < 100). The console log (' a greater than 100 ') = > > 100 aCopy the code

Special case 3 The tightening is not visible

var a = 2;
if (a===2)
console.log('a')
console.log('a=2')

=>a
Copy the code

A switch statement

  • Switch is an updated version of the if else statement

Grammar:

Switch (expression) {case n: block break; Case n: code block break; Default: default code block}Copy the code

Most of the time, do not omit the break, break will end the program, omit it will continue.

Ternary operators? :

  • Grammar :(condition)? Expression 1: expression 2
var n=99; (n % 2 === 0) ? 2. An odd number; // if (n % 2 === 0) {even = even; } else {even = odd; } = > odd numberCopy the code

6. Short-circuit logic

&& and | | short circuit logic

&& (one false all false)

A && B

  • As long as am& is false, the result returns the value before it regardless of whether it is true or false;

  • As long as am& is preceded by true, the result returns the value after am& regardless of whether it is followed by true or false;

A && B && C && D

  • The logic is to execute B if A exists; If B exists, execute C; If C exists, execute D;

  • If none exists, take the first false value, if both exist, take D.

| | (a true is true)

A || B

  • As long as the | | to false front, regardless of the | | behind is true or false, the results are returned | | at the back of the value.

  • Front is true, as long as the | | whatever | | behind is true or false, the results are returned | | the previous value.

  • A || B || C || D

The logic is to execute B if A does not exist; If B does not exist, execute C; If C does not exist, execute D;

Let’s take A for both, let’s take D for none

Conclusion:

&& is looking for false | | is to find the trueCopy the code

7. Loop statements

The while loop

  • Syntax: while(expression) {statement}
  • If the expression value is true, the statement is executed, otherwise the loop is broken.
var a = 1; while(a <= 10){ console.log(a); a++; } = > 1 2... 10Copy the code

The for loop

  • Syntax: for(statement 1; The expression; Statement 2) {statement}
  • Function:
    • Statement 1: Initialize loop variable I; \
    • Expression: if the value is true, the statement is executed, otherwise the loop is broken; \
    • Statement 2: Change the value of a loop variable (to prevent infinite loops)
for(var i = 1; i <= 10; i++){ console.log(i); } = > 1 2... 10Copy the code

8. Break and continue statements

  • Break statement
  • What it does: Jump out of the loop
var i = 10; while (i < 20){ i++; if (i % 2 === 0)break; console.log(i); } = > 11Copy the code

The code above prints 11 when I is 11 and breaks out of the loop

  • Countinue statement
  • Action: Jumps out of a single loop
var i = 10;
while (i < 20){
  i++;
  if (i % 2 === 1) continue;
  console.log(i);
}

=>11 13 15 17 19
Copy the code

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

9. Label

  • Syntax: Identifier: {statement}
  • Purpose: Identifies a loop statement so that break or continue breaks out of the loop
foo:{ console.log(1); break foo; Console. log(' no output '); } = > 1Copy the code