The article concludes from: https://wangdoc.com/javascript/basic/grammar.html
1. statement and expression
- Statement: no return value, ending with a semicolon
- Expression: required return value
- Space: no practical significance, but not recommended
- Carriage return: return cannot add carriage return
2. identifier
An identifier is a legal name used to identify various values. The naming rules are as follows:
- The first character can be any Unicode letter (including English letters and letters in other languages), as well as the dollar sign ($) and underscore ().
- In addition to Unicode letters, dollar symbols and underscores, the second character and subsequent characters can also use numbers 0-9.
- Illegal identifier: * +-
- 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, for, function, if, implements, import, in, instanceof, interface, let, new, null, package, private, protected, public, return, static, super switch,this,throw,true,try,typeof,var,void,while,with,yield.
3. Notes
- //: single line notes
- /** / multiline comment
4. block
{ }
- It is used to form other more complex syntax structures, such as for, if, while and function
- The block does not constitute a separate scope for the var command, that is, outside the block, the var declared variables are still valid
5. Conditional statement
5.1,if
if(condition){ Execute the statement when the conditions are met; Not satisfied, jump out of the loop }
eg:
if (m === 3) { m += 1; }
5.2,-if...else
if(condition){ The statement to execute when the condition is met }else{ The statement executed when the condition is not met }
eg:
if (m === 0) { // ... } else if (m === 1) { // ... } else if (m === 2) { // ... } else { // ... }
5.3,swich
Note: break cannot be less
switch (fruit) { case "banana": // ... break; case "apple": // ... break; default: // ... }
eg:
switch (x) { case 1: console.log('x Equal to 1'); break; case 2: console.log('x Equal to 2'); break; default: console.log('x Equal to other values'); }
5.4. Ternary operator?:
(condition) ? Expression 1 (if the condition is true) : Expression 2
eg: if n can be divided by 2, then a is equal to true, otherwise it is equal to false
var a = (n % 2 === 0) ? true : false;
6. Circular statement
6.1. while cycle
while (condition) { sentence; }
Monolingual sentences can be omitted {}
6.2 for cycle
for (Initialization expression; condition; increment expression ) { sentence }
- initialize expression: determines the initial value of the loop variable and is executed only once at the beginning of the loop.
- Conditional expression (test): this conditional expression must be executed at the beginning of each cycle. The cycle will continue only if the value is true.
- Increment: the last operation of each round of loop, which is usually used to increment loop variables
eg:
for(i=0;i<3;i++){ consonle.log(i) } Print content: 0 1 2
You can omit any or all of the three parts of the for statement (initialize, test and increment), but omitting them rashly will lead to a wireless loop
6.3. do...while cycle
do { sentence } while (condition); /*while Must be followed by a semicolon*/
First run the cycle body once, and then judge the cycle conditions. Whether the condition is true or not, the do...while loop runs at least once
6.4. break statement and continue statement
- break: jump out of code block or loop
- continue: immediately terminate the current round of loop and return the head of the loop structure
eg:
var i = 0; while (i < 100){ i++; if (i % 2 === 0) continue; /*If i is an even number, go directly to the next cycle.*/ console.log('i Currently:' + i); }
- Note: both break and continue statements without parameters affect the most recent loop
When
6.5. label
label, equivalent to locator, is used to jump to any position of the program. It is usually used with break statement and continue statement to jump out of a specific loop
label: sentence
eg:
//label used with break top: for (var i = 0; i < 3; i++){ for (var j = 0; j < 3; j++){ //If no label is used after the break statement, you can only jump out of the inner loop and enter 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 // 1 // 2 //label is used with continue top: for (var i = 0; i < 3; i++){ for (var j = 0; j < 3; j++){ //If no label is used after the continue statement, you can only enter the next round of inner loop, that is, 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 //label jump out of block foo: { console.log(1); break foo; console.log('This line will not output'); } console.log(2); //1 //2