This introduces basic syntax for expressions, statements, identifiers, if else and while and for loops.
Expressions, statements, and identifiers
1+2// The expression has a value of 3;
add(1.2) The value of the expression is the return value of the function
console.log // The expression is the function itself
Copy the code
Expression – refers to a calculation to get a return value.
Statement – An action to complete a task. For example, var a = 1 + 3 is an assignment statement.
The difference between expressions and statements:
- Expressions generally have values; statements may or may not
- Statements typically change the environment (declaration, assignment)
- The first two are not absolute
Identifier rule
- The first letter can be a Unicode letter or $or _ or Chinese.
- The following characters, in addition to the above types, can also be numbers.
- JS case sensitive
- Spaces and carriage returns are mostly meaningless, but do not return with carriage returns
- Note: there are // and /* */
2. Conditional statements
If syntax: if(expression){statement 1}else{statement 2}
When the expression meets the judgment condition, statement 1 is executed. Otherwise, statement 2 is executed.
A switch statement:
/*swith (variable) {case "value" : ······; break; } * /
switch (fruit) {
case "banana":
// ...
break;
case "apple":
// ...
break;
default:
// ...
}
Copy the code
In switch statements, when the variable is equal to a value of case, the following statement is executed, but each case must be terminated with a break, otherwise the rest of the statement continues.
Question mark colon expression: expression 1? Expression 2: Expression 3
Expression 2 is executed when expression 1 is true; Otherwise, expression 3 is executed.
&& Short circuit logic:
Expression 1 && expression 2 — If expression 1 is true, expression 2 is executed
A && B && C && D — take the first false value or D.
Because IE doesn’t have console, use console && console.log && console.log(‘hi’)
Short-circuit logic: | | | a | b equivalent to the if (! a){b}
C | A | B | | | | D – the first true value or D.
Three, circulation
While loop: while (expression) {statement}
If the expression is true, the statement is executed, and then the expression is checked again. When the expression is false, the current loop ends. Similarly, do… while.
For loop: for(statement 1; Expression is 2; Statement 3){body of loop}
Statement 1 — initialize; Expression 2 — Judgment; Statement 3 — increment;
When expression 2 is judged to be true, the loop body is executed and statement 3 is executed to evaluate expression 2 again.
When expression 2 is judged to be false, the loop is ended.
Break and continue
Break — Exits the current loop.
Continue — Skips the current loop.
The label tag
The statement is preceded by a label, which acts as a locator and is used to jump to any point in the program. The label has the following format
Label: statementCopy the code
Tags can also be used to jump out of code blocks, for example
foo: { / / foo label
console.log(1);
break foo;
console.log('This line will not print');
}
console.log(2);
Copy the code
The above code breaks foo to exit the block, and continue is used similarly.
{
a:1
}
Copy the code
A: is the statement label, and 1 is the statement body.
The basic syntax of JavaScript – netpath