Recently began to learn JS, understand the entry of JS usage. This article summarizes the basic syntax for getting started with JS.

Statements and expressions

Statement definition

A statement is an operation that is performed to complete a task, usually to change the environment. Such as:

var a = 1+1 ;
Copy the code

Var = 1; var = 1; var = 1; var = 1;

Definition of an expression

Expression refers to the calculation to get the return value. As in the statement example above, 1+1 is an expression.

The difference between the two

The main difference between the two is that the statement is intended for an operation and generally does not require a return value. The expression is there to get the return value, and it usually gets a value. For example, the right side of the equals sign of an assignment statement is expected to be a value, so various expressions can be placed.

variable

Variable definitions

A variable is a named reference to a value, that is, a name is given to the value, and a reference to the name is a reference to the value. The name of a variable is the variable name. Such as:

var a = 2; 
Copy the code

In the above code, variable A is first stated, and the reference relationship between variable A and value 2 is established, which is called “assigning value 2” to variable A. After the assignment, the variable name a will have the value 2. The var at the beginning of the assignment statement is a variable declaration command that tells the interpretation engine to create a variable, a.

Pay attention to the point

Note, however, that JavaScript variable names are case-sensitive, and A and A are two different variables. At the same time, most Spaces and carriage returns are meaningless without affecting the sentence break.

The only exception is that if return is not followed by a return, the system will automatically replace undefined.

Variable ascension

In case of variable promotion, the way the JS engine works is to parse the code first, obtain all the declared variables, and then run them line by line, so all variable declarations will be referred to the head of the code. Such as:

console.log(a);
var a = 1 ;
Copy the code

The code above uses the console.log method to display the value of variable A on the console. But since declaration takes precedence over assignment, the result shows undefined, indicating that variable A is declared but not assigned.

identifier

Identifiers are legal names used to identify various values. The most common identifiers are variable names and function names.

Naming rules

There is a naming convention for identifiers:

  • The first character can be a Unicode letter or a dollar sign$Or underline_Or Chinese.
  • The second character and the characters following it can be numbers in addition to the above symbols and letters0-9.

If the character is Invalid, the system automatically displays an Uncaught Syntax Error: Invalid or unexpected token.

annotation

JavaScript provides two annotation methods, one is a single-line comment starting with //, and the other is a multi-line comment placed between /* and */.

block

Blocks are groups of related statements held together by curly braces in JavaScript. For var commands, blocks do not constitute a separate scope.

Conditional statements

In JavaScript, the if structure and switch structure can be used to complete the condition judgment, that is, the corresponding statement will be executed only if the preset condition is met.

If statement

The if structure determines the Boolean value of an expression and then executes different statements based on whether the Boolean value is true. Boolean values are two special values in JavaScript, one true for true and the other false for false. The basic syntax is:

If (expression){statement 1}else if(expression){statement 2}else{statement 3}Copy the code

Among them:

  • If the expression evaluates totrueExecute the statement immediately following if the result isfalseThe following statement is skipped;
  • The expression can bea === 1, statements 1, 2, 3 can be tesselled againif;
  • elseThe code block is always the one closest to itifStatement pairing.

Be careful not to confuse the assignment expression =, equality operator ==, and strict equality operator ===.

Such as:

var x = 1;
var y = 2;
if(x = y){
    console.log(x)
}
Copy the code

The result is 2.

And here’s another example:

var a = 1; If (a === 2) console.log("a") console.log(" A = 2")Copy the code

The example above shows that a is equal to 2. If {} is omitted after an if statement, then only the first statement immediately following the if statement can be used.

A switch statement

Switch statements are a convenient substitute for multiple if else statements, but they are rarely used.

Such as:

switch(fruit){
    case"banana";
    break;
    case"apple";
    break;
    default:
}
Copy the code

Ternary operator

Ternary operators are called greetings colon expressions for short. Such as:

(conditions)? Expression 1: expression 2Copy the code

The above code means that the value of expression 1 is returned if “condition” is true, and “expression 2” is returned otherwise.

&& short circuit logic

A&&B&&C&&D 
Copy the code

The && short-circuit logic takes the first false value or D and does not take true or false.

| | short circuit logic

A||B||C||D
Copy the code

| | short circuit logic will take one of the true value or D.

Looping statements

Loop statements are used to iterate over an action.

The while loop

The while statement consists of a loop condition and a block of code that executes over and over as long as the condition is true. The basic syntax is as follows:

While (expression){statement};Copy the code

If the expression is true, the statement is executed. After the statement is executed, the expression is true or false. If the expression is false, the following statement is executed.

But there are exceptions:

var a = 1;
while(a ! == 1){
    console.log(a);
    a = a + 0.1;
}
Copy the code

This example would loop endlessly because of imprecise floating-point numbers, draining memory.

The for loop

The for statement is an updated version of the while statement, specifying the start, end, and termination conditions of the loop. The basic syntax is as follows:

For (statements 1; Expression is 2; Statement 3){body of loop}Copy the code

Statement 1 is executed and expression 2 is evaluated. If the result is true, the body of the loop is executed and statement 3 is executed again. If the result is false, the loop is broken out and the following statement is executed.

Note an abnormal exception:

for(var i = 0; i < 5; I++) {setTimeout (() = > {the console. The log (I + 'random' + math.h random ())}, 0)}Copy the code

This special case outputs the following:

Break statement

The break statement is used to jump out of a block of code or out of the current loop by taking code out of the existing order.

Such as:

var i = 0; While (I < 100){console.log(' I currently: '+ 1) I ++; if(i === 10)break; }Copy the code

The code above loops only 10 times, breaking out once I is equal to 10.

The continue statement

The continue statement also acts as a jump, but it is used to skip this cycle and start the next cycle.

Such as:

var i = 0; while(i < 100){ i++; if(i %2===0)continue; Console. log(' I currently: '+ I); }Copy the code

It is worth noting that if multiple loops exist, both the break and continue statements with no arguments are for the innermost loop only.

Label statement

A label can be placed before a statement, acting as a locator, and used to jump to any point in the program in the following format:

foo:{ console.log(1); break foo; Console. log(' this line will not output '); } console.log(2)Copy the code

When the code above is executed to break foo, it jumps out of the block and outputs 1, 2.

The above is my temporary learning of JavaScript basic syntax, basic syntax is noteworthy special case welcome to add in the comment area, thank you!

© This summary course copyright belongs to the author, reprint need to indicate the source