Expressions and statements
-
Expressions generally have values; statements may or may not have values
-
Statements typically change the environment (declaration, assignment)
Eg: a = 1 is an environment, var a = 1 is a statement
- The above two sentences are not absolute
- Only functions have return values
Eg: the 1+2 expression has the value 3
The value of the add(1,2) expression is the return value of the function
The value of the console.log expression is the function itself
console.log(3)
What is the value of the expression? isundefined
JS is case sensitive
Keep a strict distinction
Var a and var a; Function and function have different meanings
Three, Spaces,
- Most of the blanks are meaningless, as long as they don’t interrupt the sentence.
Var a=1; var a=1;
- You can also add a return, but there is one place where you can’t add a return, and if you don’t write a return and you add a return, JS will automatically add undefined.
Identifiers
(Symbols used for names)
-
Rules:
The first character can be a Unicode letter or $or _ or Chinese
eg: var _ = y ; Var Hello = ‘hi’
The following characters can also be numbers
Eg: Var $9 = x
Five, comments,
// Only one line can be commented
/* Can comment multiple lines */
Use comments correctly
- The footnote of the trample hole
- Strange code written to fix a bug
Block
Package code together with {}, often used with if/for/while
Conditional statement
1. If statement
grammar
-
If (expression) {statement 1} else {statement 2}
-
{} can be omitted in one-sentence statements, but this is not recommended
eg:
if(a < 100)
if(a < 10)
Copy the code
Abnormal situation
- Statements 1 and 2 can have nested values
if else
; - Indentation can be creepy
- Always use the least ambiguous method of writing
2. Switch statement
grammar
(Remember not to omit “break”)
switch (fruit) {
case"Banana" :/ /...
break;
case"Apple" :/ /...
break;
default:
/ /...
}
Copy the code
3. Question mark colon expression
-
Expression 1? Expression 2; The expression of 3
-
The simplest way to write it is if else, ok? Don’t use if else when:.
-
Often used to simplify if and else having only one statement. Or the absolute value of a number. Eg:
4, && short circuit logic
A && B && C && D takes the first false value or D, and does not take true /false;
5, | | short circuit logic
C | A | B | | | | D take first true value or D, does not take A true/false; Eg:
8. Loop statements
1. While loop
grammar
While (expression) {statement}
- Firstly, the expression is judged to be true or false.
- If the expression is true, the statement is executed. After execution, the expression is verified again.
- If the expression is false, the following statement is executed.
Abnormal situation
Floating point imprecision results in an infinite loop that never equals 1
2. For loop
For is a convenient way to write the while loop
Grammar:
For (statement 1; Expression 2; Statement 3){body of loop}
- Execute statement 1, then check expression 2, if true, execute the body of the loop; If false, exit the loop and execute the following statement.
Break and continue
break
Exit all loops,continue
Exits the current loop
- When you have two cycles,
break
Only the one closest to it jumps outfor
Eg:
9, label statement
Tag statements can andbreak
orcontinue
Statement used together. A tag is a statement preceded by an identifier that can be referenced.
Grammar:
foo: {
console.log(1);
break foo;
console.log('this will not be executed');
}
console.log('2');
Copy the code
Interview questions:
{
fool:1
}
Copy the code
What is the above code? A: Fool is label, and its statement is 1.
Ruan Yifeng JS grammar introduction materials
© This article is originally published by Easar. The copyright belongs to me and Hungru