01-Basic learning of JavaScript

expressions and statements

Statement: The execution unit of a JavaScript program is a line, that is, it is executed line by line. In general, each line is a statement.
This is an assignment statement

var a = 1+2

An expression refers to a calculation formula to obtain a return value; like the above 1+2, it is an expression
difference:

  • A statement is for some kind of operation, and generally does not need to have a return value; an expression is for obtaining a return value, and must return a value
  • A statement ends with a semicolon, a semicolon means the end of a statement, and multiple statements can be written in one line; expressions do not need to end with a semicolon, once a semicolon is added, it is regarded as a statement by JavaScript

Identifier Naming Rules

Identifier: An identifier is a legal name used to identify various values. The most common identifiers are variable names, and the function names to be mentioned later.
JavaScript language identifiers are case-sensitive, so a and A are two different identifiers. Identifiers have a set of naming rules, and those that do not conform to the rules are illegal identifiers. When the JavaScript engine encounters an illegal identifier, it will report an error.
The naming rules are as follows

  1. The first character can be any Unicode letter (including English letters and letters from other languages), as well as dollar signs ($) and underscores (_).
  2. The second character and subsequent characters, in addition to Unicode letters, dollar signs and underscores, can also use numbers 0-9.
//一下这些都是合法的标识符
arg0
_tmp
$elem
π
中文也是合法的标识符

Invalid identifiers are as follows

1a  // 第一个字符不能是数字
23  // 同上
***  // 标识符不能包含星号
a+b  // 标识符不能包含加号
-d  // 标识符不能包含减号或连词线

javascript还有一些保留字不能做标识符 :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。

if else statement

if else is a judgment statement

if (m === 3) {
    
    
  // 满足条件时,执行的语句
} else {
    
    
  // 不满足条件时,执行的语句
}

It is also possible to make multiple judgments

if (m === 0) {
    
    
  // ...
} else if (m === 1) {
    
    
  // ...
} else if (m === 2) {
    
    
  // ...
} else {
    
    
  // ...
}

while loop for loop

There are two kinds of while loops : the first one is to judge first and then execute, execute the statement when the condition is true, continue to judge the condition after the statement is executed, and jump out of the loop until the condition is not true

while (条件) {
    
    
  语句;
}

The second is to execute first and then judge.
The characteristic is that no matter whether the condition is true or not, at least one statement is executed.

do
  语句
while (条件);
// 或者
do {
    
    
  语句
} while (条件);

break 和continue

Both break and continue statements have a jumping effect, allowing code to be executed out of order.
the difference

  • break is used to jump out of code blocks and loops. If it is a nested loop, it jumps out of the nearest loop, but continues to execute the outer loop
  • The continue statement is used to immediately terminate the current cycle, return to the head of the loop structure, and start the next cycle.

label label

The JavaScript language allows a label (label) in front of the statement, which is equivalent to a locator, and is used to jump to any position in the program. The format of the label is as follows.

label:{
    
    
  语句
  }
  
  //或者是
  {
    
    
    a:1 
  }
复制代码

Labels are usually used in conjunction with break statements and continue statements to break out of specific loops.

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

The above code is a double-loop block, and the top label is added after the break command (note that top does not need quotation marks). When the conditions are met, it will directly jump out of the double-layer loop. If no label is used after the break statement, it can only jump out of the inner loop and enter the next outer loop.

Meaning and usage of && and ||

&& is a short-circuit logical operator that does not take Boolean values; returns the first false value or D

A && B && C && D

|| is a short-circuit logical operator that returns the first true value or the last D

A || B || C || D

js two interview questions

var =0.1
while(a!==1){
    
    
    console.log(a)
    a=a+0.1
}

It will cause an infinite loop, because the floating-point numbers are not accurate, and the sum will not be equal to 10


[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-jyohAOzp-1605107677557)(ht ]
ps://img-blog.csdnimg.cn/20201111231432964.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2J1cnV5YW5n,size_16, color_FFFFFF,t_70#pic_center)

The second way: write the result of the printout

for(i=0;i<5;i++){
    
    
    setTimeout(()=>{
    
    
        console.log(i)
    },0)
}

The printed results are not 0, 1, 2, 3, 4; but 5 5s
because setTimeout is a function that delays the execution of the specified time; js is single-threaded, all tasks need to be queued, and will be executed after the previous task is completed the next task.
When the js code execution encounters setTimeout(fn, millisec), it will put the fn function in the task queue, and wait until the js engine thread is idle and reaches the time specified by millisec, then it will put fn in the js engine thread for execution.
The meaning of setTimeout(fn,0) is to specify a task to be executed at the earliest available idle time of the main thread, that is to say, to execute the
for loop as early as possible after i=5, and to execute the function body after a while What is printed at this time is
the detailed analysis of the i=5 setTimeout function


									 学习的时光总是短暂,又到了时候说拜拜
											欢迎关注公众号  oldCode
												获取新鲜教程资料

insert image description here

Guess you like

Origin blog.csdn.net/buruyang/article/details/109633638