Re-consolidate JS series, are more basic things, can be checked to fill gaps, quickly read, this is the second.

Other JS reconsolidation series:

  • Re-consolidate JS (I) — JavaScript primitive types
  • Reconsolidate JS (3) — JavaScript statements

1. The expression

1.1 Original Expression

3.14  // Digital direct quantity
"hello world"  // String direct quantity
/pattern/  // Regular expression direct quantity

true  // Returns a Boolean value: true
false  // Returns a Boolean value: false
null  // Returns a value: null
this  // Returns the current object

i  // Returns the value of the variable I
sum // Return the value of sum
undefined  //undefined is a global variable, unlike null, which is not a keyword
Copy the code

1.2 expression

Operands and operators.

1
1 + 2
(1 + 2) * 3
(1 + 2 ) * 3 && !flag 
Copy the code

2. The operator

  • Arithmetic operator(+, -, *, /, %, **) : ordinary addition, subtraction, multiplication, division and mod operations, where支那forPower operator.
 3支那2 // Result is: 9
Copy the code
  • Unary operator(++, --, +, -) : some operators that require only one operand,Pay attention to:+ + and -The difference between the left and right sides of a variable, on the left side of a variable, increases (decreases) first and then evaluates, on the right side, the opposite.
let a = 2, y, z;
y = ++a // if a = a + 1, a = 3, then y = 3
z = a++ / / a = 3 above, will be a to z, z = 3, and then execute a = a + 1, a = 4

// unary + (+)
+3  / / 3
+'3'  / / 3
+true  / / 1
+functioan(a){return a}  //NaN
Copy the code
  • The assignment operator(=, +=, -=, *=, /=, %=) : Assigns the value of a constant or variable or expression to another variable,Note: new destruct assignment in ES6.
// Unpack the array
let [a, b, c] = ['one'.'two'.'three']  // a = 'one', b = 'two', c = 'three'
// Deconstruct the object
let { name, age } = { name: 'cc'.age: 100 }  // name = 'cc', age = 100
Copy the code
  • Equality operator(= =,! = = = =,! = =) : is used to compare whether the left and right operands are equal. May refer to:JS comparison operator ("= = =" and "= =") and the judgment result of if() condition.
  • Relational operator(>, <, > =, < =) : compares the size of the operands around the operator.
  • An operator(&, | ~ ^, < <, > >, > > >) : After converting its operand to binary, and, or, not, xor and other operations are performed, and finally return a value in JS. Among them>>>forUnsigned shift to the right.
  • Logical operator(, &&, | |!) : Determines whether an expression is true mainly by and, or, or not.Pay attention to: and and or operation has short-circuit calculation effect.
// Short circuit calculation
false && (anything) // The result is false
true || (anything) // The result is anything

// Method 1: Set the default value of x
function test(x) {
    x = x || 100
}
test(10)  //x = 10
test()  // x = 100

// Method 2: ES6
function test(x = 100) {... } test(10)  //x = 10
test()  // x = 100
Copy the code
  • Conditional operator(? :) : the only operator in JS that has three operands, usually used in short forms of if statements.