This is the second day of my participation in the August More Text Challenge
Operators solve data manipulation problems
Operators and operands
Operand: The data involved in the operation, also called “elements “.
Operators do not have to have a single match, and different problems with operators may have different meanings.
1 + 3 // The + represents the sum of 1 and 3
+1.3 // The + here means 1.3 is an integer
Copy the code
Common operators
The operator | meaning |
---|---|
= |
Assignment symbol that assigns data from the right to the left |
. |
Access symbol used to access properties of an object |
[] |
Access symbols (object expressions) that are used to access properties of objects |
(a) |
Call a function or change the order of operations |
Operator classification
By operand number:
- Unary operators:
(a)
、.
,[]
,++
,--
- Binary operators:
+
、-
,/
,*
,%
,=
- Ternary operators:
? :
The most modern browser console environment is the REPL(Read Eval Print Loop) environment, real-time input and output environment
Arithmetic operator
Arithmetic operators are symbols used to perform mathematical operations, such as + – * / %
Matters needing attention
1. In JS, the operation of decimals is not exact. Such as:
2. Divisor is 0 in js:
- If the dividend is positive, you get Infinity.
- If the dividend is negative, you get -infinity
- If the divisor and dividend are both 0, we get NaN(Not a Number)
3. Modular operation
The sign of the result depends on the dividend. Such as:
4. Implicit typesNumber
conversion
boolean
: true —> 1, false —> 0
string
: If the internal data is a correct number, remove it directlyThe first space is converted to a number, if it is any other string exceptInfinity
Anything outside of this keyword is converted directly toNaN.
null
: null —> 0
undefined
: undefined —> NaN
object
: object —-> ‘[object object]’ —-> NaN, but with a special, if array, yes0
function
: function —-> NaN
5. Plus operator
If the plus operator is used before the fourth expression, the hermit conversion of Number is called.
As long as either side is a string, it is used as a hyphen
Since the increase(+ +)
And the reduction of(-)
++
: increments its value by 1--
: Subtracts its value by 1
Matters needing attention
The priorities of autoincrement and autodecrement are particularly high. The arithmetic operations are performed in the following order: ++, — > * / % > + – > =
x++
: Increments the variable x by 1, and the value of the resulting expression isSince the increase beforeThe value of the.
++x
: Increments the variable x by 1, and the value of the resulting expression isSince the increaseThe value of the.
-
X –: Decrement the variable x by 1. The resulting expression is the value before the decrement.
-
–x: Subtract the variable x by 1, and the resulting expression is the value of the subtraction.
-
The details of the priority operation
- Look from left to right
- If you encounter operands, pull the data straight out
- If two adjacent operators are encountered and the one on the left has a higher priority than the one on the right, the left operator is run directly, otherwise the right operator is run.
Interview questions
Let’s start with an easy one
var x = 1;
console.log(x + x++ + ++x);
Copy the code
As follows:
var x = 1; X +x ++ + ++x first step: the order of + operators is less than ++, x++ first, but x++ is the value before increment, x =2
1 + 1Step 2: The + and + operations are in the same order, from left to right x =2
2++ + x Step 3: the operation order of + is less than ++, calculate ++ first and the value x = after the increment3
2 + 3The end result is that5
Copy the code
Let’s do something a little more complicated
var x = 1;
console.log(x + x++ * ++x * (x = x ++ + x + ++x) * ++x + ++x);
Copy the code
As follows:
This one has parentheses, we just have to think of the parentheses as a whole, but we’re still going from left to right.
x = 1
x + x++ * ++x * (x = x ++ + x + ++x) * ++x + ++x
x = 1Step 1: From left to right, the + operator is less than the ++ operator, so x++ first, x++ is the value before increment. And I'm going to assign the first digit x equals2
1 + 1* * + + x (x = x + + + + + + x x) * x + + + + + x the second step: + and *, * of high priority, behind. * and ++ have the highest precedence of ++, and need to calculate ++x first, since the increment of x =3
1 + 1 * 3* (x = x + + + + + + x x) * x + + + + + x step 3: * and * priority, first calculate the previous * x =3
1 + 3* (x = x ++ +x ++ +x) * ++ +x ++ ++ +x) * ++ +x ++ ++ +x step 4: the precedence of + and * is higher than that of *. The parentheses = and ++ are the higher precedence of ++, so x++ in parentheses is incremented first and then taken from the value before incrementing. x =4
1 + 3 * (x = 3+x ++x) * ++x ++ +x4The following + and + priority is the same, calculate the preceding + x =4
1 + 3 * (x = 7Step 6: Calculate the ++x in parentheses, increment the following value, and then finally assign x x=12
1 + 3 * 12* ++x ++ +x step 7: The priority of * and * is the same, calculate the preceding * x=12
1 + 36* ++x ++ +x step 8: the priority of * and ++ is higher than ++x, first calculate ++x, increment after the value x=13
1 + 39 * 13Step 9: + and * are the priorities of * x=13
469Step 10: Calculate the following ++x =13
469 + 14
483
Copy the code
Comparison operator
Comparison operators are divided into: size comparison: > < >= <=, equality comparison: ==! = = = =! = =,
- The return type of the comparison operator is
boolean
- Arithmetic operators have precedence over comparison operators
Size considerations
- The two string size comparisons areIn turn,To compare the
ASCII
console.log('11' > '2') // false
Copy the code
- If two data types are primitive (as long as one is not a string), the data will be called
Number
Make the transformation. (NaN andAny dataIf you compare them, you get all of thesefalse
)
- If the data is of a reference type, the reference type needs to be converted to the original type and then compared.
Equal comparison notes
== Compare two data is equal,! = Compare two data are not equal
- The data types on the left and right sides are the same, and the two data types are directly compared to see if they are the same.
- The data on both ends is different
null
和undefined
They are directly equivalent to other primitive types,Not equal to the
- NaN compared to any value
false
Infinity is just equal to itself
- Reference types are compared to primitive types. Reference types need to be cast to primitive types for comparison.
- Other primitive types are called automatically when compared
Number
To convert
Because the = =! = Comparison can have a lot of weird problems, in development, we usually use strict comparison ===! = =
= = =
(strictly equal) returns if the data types on both ends must be consistenttrue
, or forfalse
! = =
(strictly unequal) returns if the data types on both ends are inconsistenttrue
, or forfalse
Logical operator
Logical operators (Boolean operator) consists of the following:, &&, | |,! Null, undefined, false, NaN, ”, 0
And (and)
Symbol: &&
** syntax: **
expression1&& expression2&&...Copy the code
Boolean test expression 1, return the result of expression 2 if expression 1 is true, return expression 1 if expression 1 is false, do not execute expression 2.
or
Symbol | |
** syntax: **
expression1| | expression2 || ...
Copy the code
Boolean test expression 1, return expression 1 if expression 1 is false, do not execute expression 2, return expression 2 if expression 1 is true
non
Symbol!
** syntax: **
! expressionCopy the code
When the expression is inverted, the result must be a Boolean
Ternary operator
** syntax: **
expression1? expression2Expression:3
Copy the code
Boolean Boolean Boolean Boolean Boolean Boolean Boolean Boolean Boolean Boolean Boolean
The compound assignment operator
There are other assignment operators besides =
var x = 1;
Copy the code
+ =
(plus or equal) : x += 1 = x = x + 1;- =
(minus or equal to) : x -= 1 = x = x – 1;* =
X *= 10 + 1 = x = x * (10 + 1);/ =
X /= 10 + 1 = x = x/(10 + 1);% =
(mod =) : x %= 10 + 1 = x = x % (10 + 1);
Void operator.
The void operator is a unary operator. Returns undefined by running the expression
grammar
voidexpressionvoid(Expression)Copy the code
This operator is similar to typeof and can be written in two ways.
Comma operator
Evaluates multiple expressions in sequence, returning the last expression, with the comma operator having lower precedence than assignment
grammar
expression1The expression2
Copy the code