The difference between expressions and statements
Generally speaking, a line in JavaScript is a statement.
1+2// The expression has a value of 3; Add (1,2) // the value of the expression is the function return value console.log // the value of the expression is the function itselfCopy the code
- Expression: refers to a calculation to get a return value.
- Statement: An action to accomplish a task. Such as
var a = 1 + 3
It’s just a line of assignment.
The difference between the two:
- 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.
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 (_). —–> underline although can, but preferably within two
- 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.
Conditional statements
1. The structure of the if
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; } // Execute the following statement only if m = 3Copy the code
2. 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) {if (m === 3) {else {if (m === 3) {if (m === 3)} else {if (m === 3)}Copy the code
Looping statements
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.
var i = 0; While (I < 100) {console.log(' I currently: '+ I); i = i + 1; } // When I < 100, the loop will continueCopy the code
2. The for loop
The for statement is another form of the loop command that specifies the start, end, and termination conditions for the loop.
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.
var x = 3; for (var i = 0; i < x; i++) { console.log(i); } // 0 // 1/2Copy the code
3. 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
var i = 0; While (I < 100) {console.log(' I currently: '+ I); i++; if (i === 10) break; } // The code only executes the loop 10 times and breaks out of the loop once I = 10Copy the code
The continue statement is used to immediately terminate the loop and return to the head of the loop structure to start the next loop.
var i = 0; while (i < 100){ i++; if (i % 2 === 0) continue; Console. log(' I is currently: '+ I'); } // The code will only continue executing if I is odd, printing the current value, otherwise the loop continuesCopy the code
4. Label (lable)
The statement is preceded by a label, which acts as a locator to jump to any point in the program.
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.