Expressions and statements
Statement:
JavaScript programs are executed in units of line, that is, line by line. In general, each line is one statement.
Var a = 1 is a statement that does something to accomplish something.Copy the code
Expressions:
Var a = 1+2 where 1+2 is an expression and its operation is assigned to 3 and the result is given to variable ACopy the code
1+2 is an expression to get the return value, the return value of the function.
The value of the console.log expression is the function itself.
Differences between the two:
-
Statements are used to perform an operation and generally do not return a value.
-
Expressions are used to get a return value, and they usually return a value.
-
Statements generally change the environment (declaration). The assignment)
Of course, the above is not absolute
Rules for identifiers
An identifier is a legal name used to identify various values. The most common identifiers are variable names and, later, function names. Identifiers in the JavaScript language are case sensitive. so
Var a and var a are different
Object and object are different
Function and function are different
There is a naming convention for identifiers, and those that do not conform to the rules are illegal identifiers. The JavaScript engine encounters an invalid identifier and reports an error. The naming rules for logos are as follows:
-
The first character can be any Unicode letter (including English and other languages), as well as the dollar sign ($) and underscore (_).
-
The following characters can also be numbers in addition to the above expressions.
-
var _=1
-
var &=2
-
Var _______ =6 can be used, but it is best not to use this one
-
Hi var = ‘hi’
All of the above are valid identifiers
annotation
Comments can be written in two ways: // and /**/
// is a single-line comment /* is a multi-line comment */Copy the code
Block block
To wrap code, often with if/for/while
{
let a = 1
let b = 2
}
Copy the code
If the else statement
The if structure determines the Boolean value of an expression and then executes different statements based on whether the Boolean value is true or false. Boolean values are JavaScript’s two special values, true for true and false for false.
Note: Do not confuse the assignment expression (=) with the strict equality operator (===) with the equality operator (==).
- (=) assignment expressions do not provide comparative equivalence and use the strict equality operator (===)!!
An if block can be followed by an else block to indicate the code to execute if the condition is not met.
If (expression) {// if statement 1 meets the condition, execute the statement}else{// If statement 2 does not meet the condition, execute the statement}Copy the code
Multiple if else statements can be written together to compare the recommended method of writing a variable multiple times.
If (expression) {// statement... } else if (expression) {// statement... } else {// statement... }Copy the code
Some abnormal condition
- A is equal to 1 in this expression
- Statement 1 nested if else
- Statement 2 is also nested if else
- Indent can also appear under the trap
The second recommended way to write:
Function fn(){if(expression){return expression}if (expression){return (expression)}return (expression)} function fn(){if(expression){return (expression)}return (expression)}Copy the code
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.
grammar
While (expression) {statement; }Copy the code
-
Determine whether the expression is true or false
-
When the expression is true, the statement is executed and the expression is checked again after execution.
-
When the expression is false, the following statement is executed
The sample
var i = 0; While (I < 10) {console.log(' I currently: '+ I); i = i + 1; } the above code loops 10 times until it equals 10.Copy the code
Infinite loop example
while (true) { console.log('Hello, world'); } The above example is an infinite loop because the loop condition is always true.Copy the code
The for loop
The for statement is another form of the loop command that specifies the start, end, and termination conditions for the loop.
grammar
For (statements 1; Expression is 2; Statement 3){body of loop}Copy the code
- After the for statement, statement 1 is executed first.
- And then look at expression 2.
- If true, the body of the loop is executed and then statement 3 is executed.
- If false, exit the loop and execute the following statement.
Break statement and continue statement
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 < 20) {console.log(' I currently: '+ I); i++; if (i === 5) break; } The above code will only execute the loop 5 times and will break out of the loop once I equals 5.Copy 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 currently: '+ I); } The above code outputs the value of I only if I is odd. If I is even, it goes straight to the next cycle.Copy the code
label
grammar
foo: { console.log(1); break foo; Console. log(' this line will not output '); } console.log(2); The above code executes to break foo and jumps out of the blockCopy 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.
prompt
So the next block of code, instead of an object, should be a label with a 1 in it, so it’s going to be a 1.
{
foo : 1
}
Copy the code