The difference between JS expressions and statements is summarized as follows:

1. How do you do?

An expression is a syntactic structure made up of operators that operate to produce a result. Each expression produces a value, which can be placed wherever a value is needed, such as as an argument to a function call. Each line of code below is an expression:

var a = (5 + 6) / 2; Var b = (function(){return 25; }) (); Function (){return 25; })() foo(a*b); // expression: a*bCopy the code

2. Statements (statements)

Statements are made by “;” Separate sentences or commands. If you add a “; “after the expression This is called an “expression statement”. It represents a statement that has only expressions and no other syntactic elements.

var a = (5 + 6) / 2; If (a>12) {statements} var o = {}; Function (obj){obj.b = 23; })(o||{}); // Expression statementCopy the code
Statements in javascript generally fall into the following categories:

(1) Declaration statement: variable declaration and function declaration

(2) Assignment statement

(3) Control statements: can change the execution order of statements, including conditional statements and circular statements.

(4) expression statement: these statements remove the last semicolon, can also be used as an expression. Common are: object operations (new, delete), function calls (function execution, must return value) and so on.

Example:
var num = 9; Var I =1; var I =1; var I =1; i<10; I ++) {if(I ==num){break vloop; }else{ num = num - 1; } } } console.log(num); // Expression statement, output: 5Copy the code

There is a big difference between expressions and statements, but expressions are part of statements, and statements form executable blocks. In general, we can see the difference between the two intuitively, but some special cases are not good to distinguish.

Rules for identifiers

In JS, all names named by us can be called identifiers, for example: variable names, function names, attribute names are identifiers

Naming an identifier follows the following conventions:

  1. The first character of an identifier must be a letter, underscore _, or dollar sign $, followed by letters, digits, or underscore, dollar sign;

  2. User-defined identifiers cannot have the same name as keywords and reserved words in JavaScript, but can contain keywords or reserved words.

  3. Identifiers cannot contain Spaces;

  4. The identifier cannot contain special characters such as +, -, @, and #.

  5. There are two main ways to name compound identifiers that consist of multiple words:

    (1) Use underscores to connect each word in lower case, for example, dept_name.

    (2) the use of hump, which is divided into large hump and small hump. The format of the big hump is to capitalize the first letter of each word and lowercase the rest of the letters, for example: DeptName; The format of the small hump is all lowercase for the first word, uppercase for each word at the beginning of the second word, and lowercase for the rest of the letters, such as deptName.

Third, the if… else… Conditional statements

Grammar:

Premise: A condition

Use if to specify the block of code to execute if the condition is true

Use else if to specify the new condition to test if the first condition is false

Use else to specify which block of code to execute if the preceding conditions are all false

If (condition 1) {code block executed when condition 1 is true} else if (Condition 2) {code block executed when condition 1 is false and condition 2 is true} else {both condition 1 and condition 2 are false Block of code executed when}Copy the code

While and for loops

The while loop

The while loop loops through the code block as long as the condition specified is true

While (condition) {block of code to execute}Copy the code

The Do/While loop is a variant of the While loop. This loop executes a block of code once before checking if the condition is true, and then repeats the loop whenever the condition is true.

Do {block of code to execute} while (condition);Copy the code
The for loop

(1) Ordinary for loop

for (let index = 0; index < myArray.length; index++) {
  console.log(myArray[index]);
}
Copy the code

(2) forEach loop

myArray.forEach(function (value) {
  console.log(value);
});
Copy the code

Disadvantages: Unable to break loops (using break statements or return statements

(3) for-in loop

For-in loops are actually designed for looping an “Enumerable” object:

var obj = {a:1, b:2, c:3}; for (var prop in obj) { console.log("obj." + prop + " = " + obj[prop]); } / / output: / / "obj. A = 1" / / "obj. B = 2" / / "obj. C = 3"Copy the code

We can also use it to loop through an array:

For (var index in myArray) {// Not recommended console.log(myArray[index]); }Copy the code

It is not recommended to loop through an array with a for-in because, unlike an object, an array’s index is an important numeric sequence indicator, unlike a normal object property.

In short, for-in is the method used to loop over objects with strings of keys.

(4) for-of loop

It is simpler than the traditional for loop and makes up for the shortcomings of forEach and for-in loop.

  • Loop through an Array:
let iterable = [10, 20, 30];

for (let value of iterable) {
  console.log(value);
}
Copy the code
  • Loop a string:
let iterable = "boo";

for (let value of iterable) {
  console.log(value);
}
Copy the code
  • Loop through a TypedArray (TypedArray) :
let iterable = new Uint8Array([0x00, 0xff]);

for (let value of iterable) {
  console.log(value);
}
Copy the code
  • Loop through a Map:
let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);

for (let [key, value] of iterable) {
  console.log(value);
}

for (let entry of iterable) {
  console.log(entry);
}
Copy the code
  • Loop a Set:
let iterable = new Set([1, 1, 2, 2, 3, 3]);

for (let value of iterable) {
  console.log(value);
}
Copy the code
  • Loop through a DOM collection
let articleParagraphs = document.querySelectorAll("article > p");

for (let paragraph of articleParagraphs) {
  paragraph.classList.add("read");
}
Copy the code
  • Loop through an object that has an Enumerable property

For-of loops can’t be used directly on ordinary objects, but if we loop by properties that the Object has, we can use the built-in object.keys () method:

for (var key of Object.keys(someObject)) {
  console.log(key + ": " + someObject[key]);
}
Copy the code
  • Loop a generator
function* fibonacci() { // a generator function let [prev, curr] = [0, 1]; while (true) { [prev, curr] = [curr, prev + curr]; yield curr; } } for (let n of fibonacci()) { console.log(n); // truncate the sequence at 1000 if (n >= 1000) { break; }}Copy the code

Break and continue

The break statement “jumps out” of the loop, and the continue statement “skips” an iteration within the loop.

Break statement
  • Use to jump out of switch statements.

  • Used to break out of a loop.

  • Break the loop and continue executing the code after the loop

for (i = 0; i < 10; i++) { if (i === 3) { break; } text += "number is" + I + "<br>"; }Copy the code

The example above refers to breaking the for loop when I equals 3.

The Continue statement

The continue statement interrupts one iteration (in a loop) and then continues to the next iteration in the loop.

for (i = 0; i < 10; i++) { if (i === 3) { continue; } text += "number is" + I + "<br>"; }Copy the code

In the example above, skip the loop when I is equal to 3.

Sixth, the label

Both the break and continue statements can be used in conjunction with Lebel statements to return to specific locations in code. Can be used to reduce the number of loops in nested loops.

var num=0; outermost: for(var i=0; i<10; i++){ for(var j=0; j<10; j++){ if(i==5 && j==5){ break outermost; } num++; } } console.log(num); / / 55Copy the code

The normal loop num is 100, while the above loop is 55

var num=0; outermost: for(var i=0; i<10; i++){ for(var j=0; j<10; j++){ if(i==5 && j==5){ continue outermost; } num++; } } console.log(num); / / 95Copy the code

Although both the break and continue statements can perform complex operations as well as the Lebel statements, they can also cause problems with debugging if overused. It should be used appropriately according to the actual business