Arithmetic operators

Number operation

  • subtracting+“”-“”*“”/

(Division cannot divide by 0, Infinity)

  • remainder x % 7

If you divide two figures by 7, they have the same remainder.

-1%7 === 6%7 = false)

  • index x ** 3
  • Since the increase since decreases x++ / ++x / x-- / --x

Formula: the first value of a is before, and the second value of a is after

var a = 1
a++ //1 returns the value before a is added
a //2 increases by 1
var a =1
++a  // 2 returns the value of a plus
a / / 2
Copy the code

Use autoincrement and subtraction as little as possible, easy to misremember, a++ can be written as a+= 1

Do not add different types, JS does not raise error eg: ‘1’+ 1

  • Evaluate operator +x
  • The negative number operator -x
var a = 8
-a //-8 becomes negative
var a = -8
-a //8 minus minus is plus
Copy the code

The string operations

  • Connect operation
'123' + '456'  / / '123456'
Copy the code

Comparison operators

> | < | > = | < = | = = (fuzzy equal) |! = (range) | = = = (congruent) |! ==(incongruent)

  • JS Trinity

Never use ‘= =‘,用’= = =‘instead,’ == ‘will automatically cast

= = =‘There is no mystery. Primitive types look for value equality, and objects look for address equality.

[]! = = []// Arrays can only compare addresses, not content{}! = = {}NaN! = =NaN  / / special case
Copy the code

Boolean operators

  • or||
  • and&&
  • non !
  • Short circuit logic
console && console.log && console.log('hi') 
// In case console does not exist, if console exists, console.log(hi)

console? .log? ('hi') //JS latest version of the above code shorthand, optional chain syntax
Copy the code
a = a || 100  // The guaranteed value of a is 100, but this is a bug
function add (n=0) // The latest syntax is to write the minimum value in the argument, to solve the null judgment errorbug
Copy the code

Binary operators

  • or |If both bits are 0, the result is 0, otherwise 1
eg: (0b1111 | 0b 1010).toString(2)  / / "1111"
Copy the code
  • with &If both bits are 1, the result is 1; otherwise, 0
eg: (0b1111 & 0b 1010).toString(2)  / / "1010"
Copy the code
  • No ~ if it is 1, it becomes 0, if it is 0, it becomes 1, but when it is changed, it will be translated as negative in the form of complement

  • If the xor ^ bits are the same, the result is 0, otherwise, 1

eg: (0b0111 ^ 0b1010).toString(2) / / "1101"
Copy the code
  • Left moves to the right <<>>
eg: (0b0111 >> 1).toString(2) / / "0011"
    (0b0101 << 1).toString(2) / / "1010"
Copy the code
  • The right shift operator for header zeroing >>>

Bit operation

The use and&Operator determines even and odd

Even & 1 = 0; Odd & 1 = 1

eg: 123 & 1  / / 1
    124 & 1  / / 0
Copy the code

use~.> >, <<.>>>.|To take the whole

console.log(~~ 6.83)    / / 6
console.log(6.83 >> 0)  / / 6
console.log(6.83 << 0)  / / 6
console.log(6.83 | 0)   / / 6
console.log(6.83 >>> 0)   / / 6Bitwise operations eliminate the fractional partCopy the code

Swap a and b with alpha

var a = 5
var b = 8
a ^= b
b ^= a
a ^= b
console.log(a)   / / 8
console.log(b)   / / 5
Copy the code

Process:

JS new syntax can be written as[a,b] = [b,a]You can swap the values of A and b

5. Other operators (Weird)

Some operators

  • grammar

Property name = property value

  • role

Reads/sets the property value of the object

  • There is a question

Why can we have properties that are not objects? ‘a-b-c’.split(‘-‘)

JS has special logic, the dot is not an object, it is wrapped as an object

The number will become the number object

A string becomes a string

Bool becomes Boolean

Programmers never use these three objects, just simple types

var a = {name:'xxx'}
a.name //" XXX "reads the property value
a.name = 'yyy'  // Set the property value
Copy the code

Void operator.

  • grammar

Void expression or statement

  • role

Evaluate an expression, or execute a statement and void always has undefined

  • demand
<a href="http://example.com" onclick="f(); return false;"> click < / a >// Return false prevents default actions
<a href="javascript:; void(f())">The text</a>
// Use void to show off skills
Copy the code

Comma operator

  • grammar

Expression 1, expression 2… , expression n

  • role

Take the value of expression n as the value of the whole

  • use
let a = (1.2.3.4.5)
// The value of a is 5.
let f = (x) = > (console.log(The square of theta is theta.), x*x)
// You can write two statements without returning. Print first and evaluate later. Note that the parentheses above cannot be omitted
Copy the code

Operator priority

  • skills

Parentheses have the highest precedence, commas the lowest, and nothing else is remembered