JavaScript operators
in Note with 4 comments
JavaScript operators
in Note with 4 comments

JavaScript has the following types of operators

JavaScript has binary and unary operators, and a special ternary operator (conditional operator)

Assignment Operators

An assignment operator assigns a value based on its right operand to its left operand

name shorthand Equivalent to
assignment Assignment x = y , x = y
Addition assignment Addition assignment x += y , x = x + y
Subtraction assignment Subtraction assignment x -= y , x = x - y
Multiplicative assignment Multiplication assignment x *= y , x = x * y
division assignment Division assignment x /= y , x = x / y
Complementary assignment Remainder assignment x %= y , x = x % y
Exponentiation assignment Exponentiation assignment x **= y , x = x ** y
Left shift assignment Left shift assignment x <<= y , x = x << y
Right shift assignment Right shift assignment x >>= y , x = x >> y
Unsigned right shift assignment Unsigned right shift assignment x >>>= y , x = x >>> y
Bitwise and assignment Bitwise AND assignment x &= y , x = x & y
bitwise XOR assignment Bitwise XOR assignment x ^= y , x = x ^ y
bitwise OR assignment Bitwise OR assignment x \ = y , x = x\ y

deconstruction

Deconstructing assignment The designing assignment syntax is a Javascript expression that can extract data from the array structure or object literal corresponding to an array or object

 var foo = ["one", "two", "three"]; //Do not use deconstruction var one   = foo[0]; var two   = foo[1]; var three = foo[2]; //Use Deconstruction var [one, two, three] = foo;

Comparison operator

A comparison operator compares its operands and returns the logical value of whether an opportunity expression is true

Operands can be numbers, strings, logic, object values

The following table describes the comparison operators in the sample code

 var var1 = 3, var2 = 4;
operator describe Example of returning true
Equal (==) Returns true if the operands on both sides are equal 3 == var1 , "3" == var1 , 3 == '3'
Not equal (!=) Returns true if the operands on both sides are not equal var1 != 4 , var2 != "3"
Full Strict equal (===) Returns true when the operands on both sides are equal and the type is the same 3 === var1
Incomplete Strict not equal (!==) Returns true when the operands on both sides are not equal or the types are different var1 !== "3" , 3 !== ' 3'
Greater than (>) If the left operand is greater than the right operand, return true var2 > var1 , "12" > 2
Greater than or equal (>=) The left operand is greater than or equal to the right operand and returns true var2 >= var1 , var1 >= 3
Less than (<) If the left operand is less than the right operand, return true var1 < var2 , "2" < 12
Less than or equal (<=) The left operand is less than or equal to the right operand and returns true var1 <= var2 , var2 <= 5

Arithmetic operator

Arithmetic operators use numeric values (literal quantities or variables) as operands and return a numeric value. The standard arithmetic operator is addition, subtraction, multiplication and division (+- */)

 console.log(1 / 2); /*  prints 0.5 */ console.log(1 / 2 == 1.0 / 2.0); /*  also this is true */
operator describe Example
%(Remaining) Binary operator Returns the remainder after division 13% 5 returns 3
++(self increasing) Unary operator Adds one to the value of the operand var x=3; console.log(++x); console.log(x); var y=3; console.log(y++); console.log(y); // four thousand four hundred and thirty-four
--(self subtracting) Unary operator Subtracts the value of the operand by one var x=3; console.log(--x); var y=3; console.log(y--);
-(unary negative sign) The unary operator returns the negative value of the operand var x=3; console.log(-x);// Input-3
+(unary positive sign) The unary operator returns the positive value of the operand console.log( +'3' ); console.log( '3' ); console.log(+true); // 3 '3' 1

Bitwise Operators

Bit operators treat their operands as 32-bit binary strings (consisting of 0 and 1) rather than decimal octal or hexadecimal numbers

For example, the decimal number 9 is represented as 1001 in binary, and the bit operator performs operations on this binary representation, but the returned result is a standard JavaScript value

Bitwise logical operator

expression result Binary Description
15 \& 9 nine 1111 \& 1001 = 1001
15 fifteen 1111 | 1001=1111
15 ^ 9 six 1111 ^ 1001 = 0110
~15 -16 ~00000000...00001111 = 11111111...11110000
~9 -10 ~00000000...00001001 = 11111111...11110110

Shift Operators

The shift operator takes two operands:

The direction of the shift is controlled by the operator

operator describe
<<(left shift) Moves the first operand to the left by the specified number of bits Left out position is abandoned The bits shifted from the left are discarded. The extra bits on the right are filled by 0
>>(Move right with sign) Moves the first operand to the right by the specified number of bits The right out position is abandoned The extra space on the left is filled by the leftmost number of the original value
>>>(Shift zero filling to the right) Moves the first operand to the right by the specified number of bits The right out position is abandoned The extra space on the left is filled by 0

Logical operator

Logical operators are often used between Boolean (logical) values

When all operands are boolean, the return value is also boolean

Here is && Example of the (logical AND) operator

 var a1 =  true && true;     //  t && t returns true var a2 =  true && false;    //  t && f returns false var a3 = false && true;     //  f && t returns false var a4 = false && (3 == 4); //  f && f returns false var a5 = "Cat" && "Dog";    //  t && t returns Dog var a6 = false && "Cat";    //  f && t returns false var a7 = "Cat" && false;    //  t && f returns false

Here is || Example of the (logical OR) operator

 var o1 =  true || true;     //  t || t returns true var o2 = false || true;     //  f || t returns true var o3 =  true || false;    //  t || f returns true var o4 = false || (3 == 4); //  f || f returns false var o5 = "Cat" || "Dog";    //  t || t returns Cat var o6 = false || "Cat";    //  f || t returns Cat var o7 = "Cat" || false;    //  t || f returns Cat

Here is Example of the (logical NOT) operator

 var n1 = ! true;  // ! t returns false var n2 = ! false; // ! f returns true var n3 = ! "Cat"; // ! t returns false

String operator

(+) connected in series

 console.log( "my " + "string " + 2); //  "my string 2"

(+=) connected in series

 var myString = "alpha"; myString += "bet";

Conditional (ternary) operator

The conditional operator is the only operator in JavaScript that requires three operands

The result of the operation takes one of the two values according to the given conditions

 condition ?  val1 : val2

If condition is true, the result is val1; otherwise, it is val2

 var status = (age >= 18) ?  "adult" : "minor";

When age is greater than or equal to 18, the statement will "adult" Assign a value to status, otherwise it will "minor" Assign value to status

comma operator

The comma operator evaluates two operands and returns the value of the second operand

It is often used in the for loop to update multiple variables at each loop

 for (var i = 0, j = 9; i <= j; i++, j--) console.log("a[" + i + "][" + j + "]= " + a[i][j]);
 a = b = 3, c = 4; //  Returns 4 in console console.log(a); //  3 (left-most) x = (y = 5, z = 6); //  Returns 6 in console console.log(x); //  6 (right-most)

unary operator

The unary operator only corresponds to one operand

Delete operator

The delete operator deletes an object or an object's property or an element at a specified index in an array

 delete objectName; delete objectName.property; delete objectName[index]; delete property; //  legal only within a with statement

If the delete operation is successful, the attribute or element will become undefined If delete is feasible, it will return true. If it is unsuccessful, it will return false

 x = 42; var y = 43; myobj = new Number(); myobj.h = 4;    //  create property h delete x;       //  returns true (can delete if declared implicitly) delete y;       //  returns false (cannot delete if declared with var) delete Math. PI; //  returns false (cannot delete predefined properties) delete myobj.h; //  returns true (can delete user-defined properties) delete myobj;   //  returns true (can delete if declared implicitly)

also typeof void

Relational Operators

In operator

If the specified property is in the specified object, true will be returned

 propNameOrNumber in objectName

Common usage of in operation

 // Arrays var trees = new Array("redwood", "bay", "cedar", "oak", "maple"); 0 in trees;        //  returns true 3 in trees;        //  returns true 6 in trees;        //  returns false "bay" in trees;    //  returns false (you must specify the index number, // not the value at that index) "length" in trees; //  returns true (length is an Array property) // Predefined objects "PI" in Math;          //  returns true var myString = new String("coral"); "length" in myString;  //  returns true // Custom objects var mycar = {make: "Honda", model: "Accord", year: 1998}; "make" in mycar;  //  returns true "model" in mycar; //  returns true

Reading materials: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide

Review method: practice (write code once, run once, test once), Google where you don't understand, read and take notes

Bottom line principle: prefer rewriting once rather than copying and pasting

This review includes: operators···

Review time: it lasted for several days intermittently... I was drunk too···

Responses
  1. Is there any code highlighting plug-in? I installed several but failed...

    Reply
    1. @Fishing

      The topic does not need code highlighting plug-ins, but uses pirsmjs.

      Reply
  2. ③d

    Where can I find the vector thumbnail of the blogger?

    Reply
    1. Kaikai
      @③d

      Baidu can search the next 14 HD copyright free image website resources

      Reply