Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money
Although primary school had learned mathematics add, subtract, multiply and divide, but have you learned JS well inside add, subtract, multiply and divide? Take a look!
Ask questions
👉 What are operators, unary operators, binary operators?
👉 plus, is it a unary or a binary operator?
👉 If you want to prioritize, which is higher, unary operator or binary operator?
👉 Previous review: Talk about JavaScript variables
The campaign needs to collect 10 comments, and the current progress is still zero ðŸ˜, so
Basic terminology
Operator: The objects to which the operator applies, as in multiplication 5 * 2, 5 and 2 are called operators (more often called arguments).
Unary operator: When there is only one unary operator, it is called unary operator, as in
let x = 1; x = -x; // This is called the unary negative operator.Copy the code
Binary operators: When an operator has two elements, it is called a binary operator, as in
let x = 1, y = 3; console.log( y - x ); // 2, this is called the binary minus operator.Copy the code
Operator priority
If an expression has more than one operator, the order of execution is determined by priority.
Such as:
- expression
1 plus 2 times 2
In, multiplication precedes addition - Parentheses have the highest priority and can be used to change the priority
1 plus 2 times 2
Each operator has a corresponding priority number. The larger the number, the earlier the execution. If the priorities are the same, they are executed from left to right.
Note: Just remember that unary operators take precedence over binary operators. For more information, see 👉 Mozilla’s priority table
Math supported by JavaScript
- add
+
- subtraction
-
- The multiplication
*
- division
/
- Take more than
%
- exponentiation
**
Only special operators are described below
add+
When it is a binary operator, it can be used
-
Used mathematically for summation
-
Used on strings for string concatenation
let s = "my" + "string"; console.log(s); // mystring Copy the code
Note: As long as either operand is a string, the other operand will also be converted to a string.
console.log( '1' + 2 ); // "12" console.log(2 + 2 + '1' ); // "41", not "221", operators work sequentiallyCopy the code
-
The [+ +] operator is the only one that supports strings in this way;
-
Other arithmetic operators operate only on numbers and convert their operators to numbers;
console.log( 6 - '2' ); // 4, convert '2' to the number console.log('6'/'2'); // 3, convert both operands to numbersCopy the code
When it is the unary operator,
- Not valid for numbers
- You can turn the rest into numbers
// let x = 1; console.log( +x ); // 1 let y = -2; console.log( +y ); // -2 // Convert non-digital console.log(+true); // 1 console.log( +"" ); / / 0Copy the code
Tip: You can use this to sum string numbers
//📒 defaults to string merge let apples = "2"; let oranges = "3"; console.log( apples + oranges ); //📒 If you want to treat it like a number, you can change it to: console.log(+apples + +oranges); // 5 //📒 equals console.log(Number(apples) + Number(oranges)); / / 5Copy the code
Take more than%
A % b is the remainder of a divided by b
console.log( 5 % 2 ); Console. log(8%3); // 2, remainder of 8 divided by 3Copy the code
exponentiation**
Exponentiation a * b is a times itself b times.
console.log( 2 ** 2 ); // 4 (2 * 2, self multiplied twice) console.log(2 ** 3); // 8 (2 * 2 * 2, multiply 3 times) console.log(2 ** 4); // 16 (2 * 2 * 2 * 2)Copy the code
The assignment operator
= is also an operator called assignment, and it has a very low priority of 3, so it is usually executed last;
Such as:
Let x = 2 * 2 + 1; let x = 2 * 2 + 1; console.log( x ); / / 5Copy the code
Here’s a complicated example:
let a = 1; let b = 2; let c = 3 - (a = b + 1); console.log( a ); // 3 console.log( c ); / / 0Copy the code
In this example, (a = b + 1) results in the value assigned to A (i.e., 3). This value is then used for further operations. (It needs to be known, but it’s best not to do this in real development.)
You can also chain assignment, which evaluates from right to left
let a, b, c; a = b = c = 2 + 2; console.log( a ); // 4 console.log( b ); // 4 console.log( c ); / / 4Copy the code
Modify and assign
Quite often, we need to perform an operation on a variable and store the new result in the same variable.
Conventional writing:
let n = 2;
n = n + 5;
n = n * 2;
Copy the code
You can abbreviate this representation using the operators += and *=.
let n = 2; n += 5; // Now n = 7 (same as n = n + 5) // Now n = 14 (n = n * 2)Copy the code
Operators of this class have the same precedence as the normal assignment operator, so they execute after most other operations:
let n = 2; n *= 3 + 5; // 16 (the right-hand part is calculated first, equivalent to n *= 8)Copy the code
All arithmetic and bitwise operators have short “modify and assign” operators: /=, -=, etc
conclusion
Articles will be updated, optimized, and refactored constantly!
“This article has participated in [Digg Star Project], post a comment in the comments section of this article, you will have a chance to participate in [100 nuggets around] 💥 raffle 💥, badges, slippers, mugs, canvas bags waiting for you, 👉 details of the event”.
Reference: Basic Operators, MATHS
🎨 [thumbs up] [concerns] don’t get lost, more front-end dry goods waiting for you to unlock
Phase to recommend
👉 What’s new with the latest version of chrome developer tools?
👉 layui image collection + how to copy the whole website down
👉 Learn JavaScript from 0
Memo | 👉 HTTP status code is commonly used & request & response header & Cookies and collection request method