arithmetic operator
a = 1 + 1 // 2
a = 10 - 5 // 5
a = 10 / 5 // 2
a = 10 / 0 // Dividing by 0 in js doesn't result in an error, the result is Infinity.
a = 2*2 // 4
a = 2**2 // 4
a = 10 % 4 // Balance, 2
Arithmetic operations in js, except for addition of strings, automatically convert non-numeric values to int for calculation, unlike other languages that report errors
a = 10 + true // 11
a = 10 - "3" // 7
a = 5 + null // 5
a = 5 - undefined // NaN
String addition, which converts non-string values to strings, performs string splicing
a = "1" + a // "1a"
a = "1" + 1 // "11"
a = "1" + null // 1null
Take advantage of the implicit type conversion property of arithmetic operations, which can also be used for type conversions, such as
true +''
, use the value to add to the empty string, the result will convert true to the string's true
assignment operator
=
Used to assign a value to a variable
let a
a = 10 // Assign the value on the right to the variable on the left.
let b = a // assign a to b. A variable is only a variable if it's on the left, and a value if it's on the right.
assignment operation
// Most operators do not change the value of a variable, except for the assignment operator.
a = 10
a + 1 // a + 1 results in 11, but a is still 10, and the result is not assigned.
a += 1 // a results in 11, which is equivalent to a = a + 1, /=, *=, and so on.
??=
empty assignment
Assignment operations are performed only if the value of the variable is null or undefined
let a
a ?? = 1 // a has no value, assign 1 to a
(a) // 1
let b = 10
b ?? = 2 // b already has a value, so we don't assign 2 to b.
(b) // 2
single variable (math.)
let a = 10
// plus sign: a sign that doesn't change the value
a = +a // 10
// Negative sign: inverts the sign of the value
let b = 10
b = -b // -10
a = -a // -10
// When performing positive or negative operations on non-numeric types, they are first converted to numeric values and then operated upon
let c = "10"
c = +c // int 10, which is equivalent to b=Number(c), but you can also use the unary + conversion type
c = -c // int -10
Self-adding and self-reducing
additive
Self-incrementation is divided into pre-incrementation (variable ++) and post-incrementation (++ variable)
// Pre-increment
let a = 10
(a++) // 10
(a) // 11
// Post-increment
let b = 20
(++b) // 21
(b) // 21
// Pre- and post-incrementation both increase the value of the original variable by 1, the difference is in the return values
The difference is that the return value is different. // The return value of a pre-increment expression is the value before the increment, and the return value of a post-increment expression is the value after the increment.
self-reducing
Self-decrease is also divided into pre-self-decrease and post-self-decrease, the principle is the same as self-increase
let c = 10
(c--) // 10
(c) // 9
let d = 10
(--d) // 9
(d) // 9
logical operator
logical nonce!
!
Can perform non-arithmetic operations on a value, can perform inverse operations on a Boolean value, if a non-Boolean value is inverted, it will be converted to a Boolean value before being inverted, you can use this specific to convert other types to Boolean values
let result
result = !0 // true
logic with&&
The with operation returns true if all expressions are true, otherwise it returns false
The with operation is short-circuited; if there is a value of false, it returns false directly and does not execute backwards
let result
result = true && true // true
result = true && false // false
result = true && alert(1) // alert is executed
result = false && alert(1) // alert won't value
For non-Boolean operations, it will be converted into a Boolean value for the operation, but the final return is not a Boolean, it will return the original value
If any of the values are false, the value of false is direct, and if all the values are true, the last value is returned
let result
result = 1 && 2 && 3 // 3
result = 1 && 0 && 2 // 0
logical or||
Returns true if there is a true value around the time of the || operation, otherwise false.
The or operation is also short-circuited; if it finds true, it returns it directly and does not work backwards.
For non-Boolean operations, it will be converted to a Boolean value and then operated on, but ultimately will also return the original value, if the first is false to return the second value
let result
result = true || false // true
result = false || false // false
result = 1 || 2 // 1
result = 0 || 2 // 2
result = 1 || alert(1) // will not run alert
result = 0 || alert(1) // alert will be executed.
relational operator (computing)
Checks whether the relationship between two values is valid, returns true if valid, returns false if not.
let result
result = 5 > 5 // false
result = 5 >=5 // true
result = 5 <= 5 // true
// When performing relational operations on non-numeric values, they are converted to numeric values before being compared.
result = "1" > false // true
result = 5 < "10" // true
// When there are two strings on either side of the relational operator, the strings are not converted to values, but are compared bit by bit against their own Unicode encoding.
result = "a" < "b" // true
result = "12" > "2" // false , even if both strings are numeric, does not convert numeric operations
result = +"12" > "2" // true Numeric operations are performed if a value can be converted to a numeric value using the unary operation
equality operator
==
Equality operator, used to compare two values are equal, equal return true, otherwise false
- If two values are of different types, the equality operator converts them to the same type (usually numeric) and compares them.
- Returns true when comparing null to undefined.
- NaN is not equal to any value, including itself.
===
The equality operators are all used to compare two values for equalityIf the two values are of different types, no type conversion is performed, and if the two values are of different types, false is returned directly.
let result
// Equivalence
result = 1 == 2 // false
result = "1" == 1 // true
result = null == undefined // true
// Equality
result = "1" === 1 // false
result = 2 === 2 // true
result = null === undefined // false, undefined and null are equal, but not congruent.
!=
: Compare two values to see if they are not equal, type conversion is automatically performed
!==
:: Compare two values for incomplete equality, no automatic type conversion is performed
result = 1 != 1 // false
result = 1 != "1" // false
result = 1 !== "1" // true
Conditional operators (ternary operators)
Conditional expressions? Expression 1:Expression 2
Execution order:
- First make a value judgment on the expression, if the result is true, then execute expression 1, if the result is false, then execute expression 2
let a = 10
let b = 20
a > b ? alert("a") : alert("b")
let max = a > b ? a : b // if a is greater than b, then max = a, otherwise it is equal to b
operator priority
The operator () has the highest priority, so if you need to prioritize operations, you can wrap the expression with ()