Statements and expressions
statements
A statement is an operation performed to accomplish a task. For example, here is an assignment statement. var a = 1 + 3; This statement declares the variable a with the var command, and then assigns the result of the 1 + 3 operation to it.
Statements with semicolons; A semicolon indicates the end of a statement.
expression
Expression, “expression,” is an expression that is calculated in order to get a return value. 1 + 3 is the expression.
The difference between statements and expressions
Statements are intended to perform an operation, and generally do not return a value; In order to get a return value, an expression must return a value. Expressions can be used wherever values are expected in the JavaScript language.
identifier
An identifier is a legal name used to identify a value. The names of variables and functions are identifiers.
Naming conventions for identifiers
- The first character, which can be any Unicode letter (both English and other languages), and the dollar sign (
$
) and underline (_
). - The second character and the characters that follow can use numbers in addition to Unicode letters, dollar signs, and underscores
0-9
.
The following are valid identifiers
_name $age PI arg0Copy the code
The following are invalid identifiers
*** // Identifiers may not contain asterisks a+c // identifiers may not contain a plus sign -d // Identifiers may not contain a minus sign or a conjunctionCopy the code
Chinese is also a valid identifier and can be used as a variable name. Var name = ‘JS’
If the else statement
The if structure determines the Boolean value of an expression, executing the statement contained in the if if true.
writing1
let a = 1
if(a === 1) {// Recommend writing method to facilitate code modification
a++; //a=2} notation2
if (a === 1) a++; // a=2
if(a === 1) // This will cause the if statement to be independent of statement 2, since only the first statement will be executed after the if statement succeeds.a++; statements2
Copy the code
When the Boolean value of the if expression is false, the else code block is the code to execute.
if (a === 3) {
// The statement to execute when the condition is met
} else {
// The statement to execute if the condition is not met
}
Copy the code
When the same variable is judged multiple times, multiple if… Else statements can be written together, and the else block is always paired with the nearest if statement.
if (a === 0) {
// ...
} else if (a === 1) {
// ...
} else if (a === 2) {
// ...
} else {
// ...
}
Copy the code
var m = 1;
var n = 2;
if(m ! = =1)
if (n === 2) console.log('hello');
else console.log('world');
Copy the code
There will be no output from the above code, and the else block will not be executed because it follows the nearest if statement, as follows.
if(m ! = =1) {
if (n === 2) {
console.log('hello');
} else {
console.log('world'); }}Copy the code
If you want the else block to follow the top if statement, you change the braces.
if(m ! = =1) {
if (n === 2) {
console.log('hello'); }}else {
console.log('world');
}
// world
Copy the code
While the for loop
The while loop
The while statement consists of a looping condition and a block of code that is looping through as long as the condition is true.
/ / write 1
while(condition) statement;/ / write 2
while(condition) statement;// Standard writing method
while(condition) {statement; }Copy the code
Small example 🌰
var i = 0;
while (i < 100) {
console.log('I is currently:' + i);
i = i + 1;
}
console.log(i) //i=100
// Loop 100 times until I equals 100.
Copy the code
The for loop
The for statement (the syntactic sugar of while) is another form of the loop command that specifies the start, end, and termination conditions of the loop. Its format is as follows.
for(Initialize the expression; Conditions; Incrementing expression) statement/ / or
for(Initialize the expression; Conditions; Increment expression) {statement}Copy the code
In parentheses after the for statement, there are three expressions.
- Initialize: Determines the initial value of the loop variable and is executed only once at the start of the loop.
- Conditional expression (test) : At the beginning of each round of the loop, this conditional expression is executed, and the loop continues only if the value is true.
- Increment: The last operation of each cycle, usually used to increment a cycle variable.
Modify the while example 🌰 from ☝ above
for(let i = 0; i<100; i++){
console.log('I is currently:' + i); / / 0-99
}
console.log('I is currently:' + i); / / 100
Copy the code
Code running process
- A statement I = 0;
- Judge I < 100
- Enter the code block and output the current value of I
- i+1
- Judge I <100 and start the loop…
Break and continue statements
Both break and continue statements act as jumps, allowing code to execute out of the order it already has. The break statement is used to break out of a code block or loop. Both the while and for loops can break out of the loop using a break statement.
for (var i = 0; i < 5; i++) {
console.log(i);
if (i === 3)
break;
}
// 0 1 2 3
Copy the code
The above code will break out of the loop when I equals 3. The continue statement is used to immediately terminate the loop, return the head of the loop structure, and 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 above code 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, the break and continue statements with no arguments are for the innermost loop only.
The label (tag)
The JavaScript language allows statements to be preceded by a label, which acts as a locator to jump anywhere in the program. The format of the label is as follows. Tags are usually used in conjunction with break and continue statements to break out of a particular loop.
Label: statementCopy the code
See the example 🌰 (top is the tag) break is used with the tag.
top:
for (var i = 0; i < 3; i++){
for (var 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. The break command is followed by the top tag (note that top is not in quotes). If the condition is met, the double loop is broken. If the break statement is not followed by a label, it can only break out of the inner loop and into the next outer loop.
Continue statements are used in conjunction with labels.
top:
for (var i = 0; i < 3; i++){
for (var j = 0; j < 3; j++){
if (i === 1 && j === 1) continue top;
console.log('i=' + i + ', j='+ j); }}// 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
In the above code, the continue command is followed by a label name, and when the condition is met, the current loop is skipped and the next round of the outer loop goes directly. If the continue statement is not followed by a label, you can only go to the next round of inner loops.
Block statements are used in conjunction with labels.
foo: {
console.log(1);
break foo;
console.log('This line will not print');
}
console.log(2);
/ / 1
/ / 2
Copy the code
When the above code is executed to break foo, the block will jump out.
What makes us unhappy is never reality, but desire. Too much desire makes us empty and weak. But the true happiness of human beings comes from self-discipline and self-determination, the firm feeling of self-transcendence. The complexity and difficulty of life is often caused by wanting too much, which is a lack of self-knowledge and objectivity in life.