Identifier rule
In an application, use variables as symbolic names for values. The name of a variable is also called an identifier, and it needs to follow certain rules.
Must be in letters, underscores (_) Or start with dollar sign ($)
Subsequent characters can also be numbers (0-9).
Because the JavaScript language is case sensitive, letters can be uppercase from "a" to "Z" and lowercase from "a" to "Z".
Most ISO 8859-1 or Unicode encoded characters can be used as identifiers, such as å and ü. You can also use Unicode escape characters as identifiers.
Legal identifier example: Number_hits, temp99, $credit and_ name.
Declare variables
Use the keyword var. For example, var x=42. This syntax can be used to declare local and global variables.
Direct assignment. For example, x =42. Using this form of assignment outside a function produces a global variable. Errors can occur in strict mode. So you should not declare variables in this way.
Use the keyword let. For example, let y=13. This syntax can be used to declare block scoped local variables.
Evaluate Variable
If a variable declared with var or let statements is not assigned an initial value, its value is undefined.
I am currently working in front-end development. If you want to learn front-end development technology now, In the process of the front end of introductory learning, there are any problems about learning methods, learning routes, learning efficiency, etc, You can apply to join my front-end learning exchange skirt: Front: 851 middle: 231 last: 348. It's packed inside Some beginners who are self-learning the front-end also have some front-end learning manuals that I sorted out during the time I was working on front-end technology, Front end interview questions, front-end development tools, PDF Documents, books and tutorials can be downloaded if necessary.
Block block
Function: package the code together
{ let a=1 let b=2 }
Often used with if/for/while
if else statement
if (expression) {Statement 1} else {Statement 2}
{} it can be omitted when the sentence has only one sentence, but it is not recommended to omit.
Writing method
Use a=1 in the expression===
if (expression) { statement } else if (expression) { statement } else { statement }
function fn() { if (expression) { return expression } if (expression) { return expression } return expression }
Switch statement
swith(fruit){ case "banana": //.. break; case "apple": //... break; default: break; } }
while statement
while (expression) {statement}
Judge true and false
If the expression is true, execute it, and judge whether it is true or false after execution
If the expression is false, execute the following.
Dead cycle exception
var a=0.1 //initialization while(!a==1){ //judge console.log(a) //Circulatory body a=+0.1 //increase } }
Because floating point numbers are not accurate
for loop
Is a simple way to write a while loop
for (Statement 1; Expression 2; Statement 3) { Circulatory body }
Statement 1 is used to initialize
Expression to judge
Statement 3 to write growth
The execution order is: statement 1 – expression 2 – loop body – statement 3
If expression 2 does not hold, jump out of the loop directly
special case
Executing the following code will print 5 5
Because the value of delayed print i is 5
for (var i = 0; i < 5; i++){ setTimeout(() => { console.log(i); }, 0); }
The print value depends on when the function is called. We start a delay function setTimeout(fn), a = 5, and print 5.
Is there any way to change that?
If we replace var with let, we will print 011234.
var a = 1; function fn() { console.log(a); }
break
Exit all current cycles
continue
Exit the cycle closest to it
lable
foo: { cosole.log(1); break foo; console.log('This line will not output'); } console.log(2)
Abbreviation
foo:1 console.log(2)
Question mark colon expression
A?B:C
function max(a,b){ return a>b ? a:b }
&&Short circuit logic
A&&b&&c&&d take the first false value or D
consonle&&console.log&&console.log(hi)
||
A | B | C | d take the first true value or D