1. Differences between expressions and statements
JavaScript programs are executed line by line. In general, each line is one statement.
var a = 1+3;
Copy the code
- This statement uses the var command to declare variable A, and then assigns the result of the 1 + 3 operation to variable A.
- 1 + 3 is called an expression, and it means a calculation to get a return value
The difference between a statement and an expression is that the former is intended to perform an operation and generally does not require a return value. The latter, in order to get a return value, must return a value.
The difference between:
- Statements are used to perform an operation and generally do not return a value
- The expression returns a value, and it must return a value
Expressions can be used wherever values are expected in the JavaScript language. For example, the right side of the equals sign of an assignment statement is expected to be a value, so various expressions can be placed.
2. Rules for identifiers
- An identifier is a legal name used to identify various values. The most common identifiers are variable names and function names.
- Identifiers in the JavaScript language are case sensitive, so a and A are two different identifiers.
Naming rules
- The first character can be any Unicode letter (including English and other languages), as well as the dollar sign ($) and underscore (_).
- In addition to Unicode letters, dollar signs, and underscores, the numbers 0-9 can be used for the second and subsequent characters.
- Chinese is a valid identifier and can also be used as a variable name (not recommended)
JavaScript has some reserved words that cannot be used as identifiers: Arguments, break, case, catch, class, const, continue, debugger, default, delete, do, else, enum, eval, export, extends, false, finally, F Or, function, if, implements, import, in, Instanceof, Interface, let, new, NULL, Package, Private, protected, public, return, static, sup Er, switch, this, throw, true, try, typeof, var, void, while, with, yield.
3. Conditional statements
3.1 if construct
The if structure determines the Boolean value of the expression in parentheses and then executes a different statement depending on whether the Boolean value is true or false. Boolean values are JavaScript’s two special values, true for true and false for false.
if (m === 3) {
m += 1;
}
Copy the code
The code above says that it only adds 1 to the value if m is equal to 3.
3.2 the if… The else structure
An if block can be followed by an else block to indicate the code to execute if the condition is not met
if (m === 3) {
// The statement to execute when the condition is met
} else {
// The statement to execute if the condition is not met
}
Copy the code
The code above determines whether the variable m is equal to 3 and executes an if block if it is, and else block otherwise
4. Loop statements
4.1 the while loop
The While statement consists of a loop condition and a block of code that executes over and over again as long as the condition is true.
while(condition) {statement; }Copy the code
Ex. :
var i = 0;
while (i < 100) {
console.log('I is currently:' + i);
i = i + 1;
}
Copy the code
The code above loops 100 times until I equals 100.
4.2 a for loop
The for statement is another form of the loop command that specifies the start, end, and termination conditions for the loop. The format is as follows.
forInitialize the expression; Conditions; Incrementing expression) {statement}Copy the code
After the for statement, there are three expressions in parentheses.
- Initialization expression: Determines the initial value of a loop variable and executes it only once at the start of the loop.
- Conditional expression: This conditional expression is executed at the start of each loop, and the loop continues only if the value is true.
- Increment expression: The last operation of each loop, usually used to increment a loop variable.
Ex. :
var x = 3;
for (var i = 0; i < x; i++) {
console.log(i);
}
/ / 0
/ / 1
/ / 2
Copy the code
The for loop is executed in the order that the initialization expression is first executed (only once) > conditional expression > statement > increment expression > conditional expression…
5. Break and continue statements
Both the break statement and the continue statement jump, allowing code to execute out of the existing order.
The break statement is used to break out of a code block or loop
Ex. :
var i = 0;
while(i < 100) {
console.log('I is currently:' + i);
i++;
if (i === 10) break;
}
Copy the code
The code above only executes the loop 10 times, and breaks out once I is equal to 10
The continue statement is used to immediately terminate the loop and return to the head of the loop structure to start the next loop.
Ex. :
var i = 0;
while (i < 100){
i++;
if (i % 2= = =0) continue;
console.log('I is currently:' + i`); `
}
Copy the code
The code above only prints the value of I if I is odd. If I is even, it goes straight to the next cycle.
5. Label
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
The tag can be any identifier, but cannot be a reserved word, and the statement part can be any statement.
Tags are often used with break and continue statements to break out of a particular loop.
Ex. :
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, followed by the break command with the top tag (note that top is not quoted), when the condition is met, directly out of the double loop. If the break statement is not followed by a label, you can only break out of the inner loop and enter the next outer loop.