JavaScript Arithmetic
- Προηγούμενη σελίδα JS τύποι
- Επόμενη σελίδα JS ανάθεση
The typical scenario for handling numbers is arithmetic.
JavaScript Arithmetic Operators
Arithmetic operators perform arithmetic operations on numerical values ( literals or variables).
operator | description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
** | Power (ES2016) |
/ | Division |
% | Coefficient |
++ | Increment |
-- | Decrement |
Arithmetic operators
Typical arithmetic operations operate on two numerical values.
These two numbers can be literals:
instance
var x = 7 + 8;
Or variable:
instance
var x = a + b;
Or expression:
instance
var x = (7 + 8) * a;
Operators and operands
In arithmetic operations, numbers are calledOperands.
The operation (performed between two operands) is defined byoperatorDefinition.
Operands | operator | Operands |
---|---|---|
7 | + | 8 |
Subtraction
SubtractionOperator (-
) Subtrahend.
var x = 7; var y = 8; var z = x - y;
Multiplication
MultiplicationOperator (*
) Multiplier.
var x = 7; var y = 8; var z = x * y;
Coefficient
CoefficientOperator (%
) Returns the remainder of the division.
var x = 7; var y = 2; var z = x % y;
Note:In arithmetic, the division of two integers produces a quotient and a remainder.
Note:In mathematics, the result of the modulus operation is the remainder of the arithmetic division.
Exponent
The exponentiation operator (**) raises the first operand to the power of the second operand.
instance
var x = 5; var z = x ** 2; // The result is 25
The result of x ** y is the same as Math.pow(x,y):
instance
var x = 5; var z = Math.pow(x,2); // The result is 25
Operator precedence
Operator precedence (Operator precedence) describes the order of operations performed in arithmetic expressions.
instance
var x = 200 + 50 * 2;
What is the result of 250 * 2 or 200 + 100 in the previous example?
Is addition or multiplication given priority?
In traditional school mathematics, multiplication is given priority.
multiplication(*
)and division(%
)and addition(+
)and subtraction(-
)has a higherprecedence.
At the same time, (just like in school mathematics) you can change the precedence by using parentheses:
instance
var x = (200 + 50) * 2;
When using parentheses, the operators within the parentheses are calculated first.
When multiple operators have the same precedence (such as addition and subtraction), their calculations are performed from left to right:
instance
var x = 200 + 50 - 2;
JavaScript operator precedence values
value | operator | description | instance |
---|---|---|---|
20 | ( ) | expression grouping | (3 + 4) |
19 | . | member | person.name |
19 | [] | member | person["name"] |
19 | () | function call | myFunction() |
19 | new | create | new Date() |
17 | ++ | postfix increment | i++ |
17 | -- | postfix decrement | i-- |
16 | ++ | prefix increment | ++i |
16 | -- | prefix decrement | --i |
16 | ! | Λογική αρνητική | !(x==y) |
16 | typeof | type | typeof x |
15 | ** | power (ES7) | 10 ** 2 |
14 | * | multiply | 10 * 5 |
14 | / | divide | 10 / 5 |
14 | % | modulus division | 10 % 5 |
13 | + | add | 10 + 5 |
13 | - | subtract | 10 - 5 |
12 | << | left shift | x << 2 |
12 | >> | right shift | x >> 2 |
12 | >>> | right shift (unsigned) | x >>> 2 |
11 | < | less | x < y |
11 | <= | less than or equal | x <= y |
11 | > | greater | x > y |
11 | >= | greater than or equal | x >= y |
11 | in | property in object | "PI" in Math |
11 | instanceof | instance of object | instanceof Array |
10 | == | equal | x == y |
10 | === | strictly equal | x === y |
10 | != | Μη ισοδύναμη | x != y |
10 | !== | Στατική μη ισοδύναμη | x !== y |
9 | & | Λογική και | x & y |
8 | ^ | Λογική XOR | x ^ y |
7 | | | Λογική ή | x | y |
6 | && | Λογική και | x && y |
5 | || | Λογική αρνητική | x || y |
4 | ? : | Τελική συνθήκη | ? "Ναι" : "Όχι" |
3 | = | Ανάθεση | x = y |
3 | += | Ανάθεση | x += y |
3 | -= | Ανάθεση | x -= y |
3 | *= | Ανάθεση | x *= y |
3 | %= | Ανάθεση | x %= y |
3 | <<= | Ανάθεση | x <<= y |
3 | >>= | Ανάθεση | x >>= y |
3 | >>>= | Ανάθεση | x >>>= y |
3 | &= | Ανάθεση | x &= y |
3 | ^= | Ανάθεση | x ^= y |
3 | |= | Ανάθεση | x |= y |
2 | yield | Παύση συνάρτησης | yield x |
1 | , | Κόμμα | 7, 8 |
Σημείωση:Καταχρώμενη ή προτεινόμενη τεχνολογία (ECMASScript 2016 ή ES7)
Συμβουλή:Η έκφραση εντός των κούκλων θα υπολογιστεί πλήρως πριν χρησιμοποιηθεί στην υπόλοιπη έκφραση.
- Προηγούμενη σελίδα JS τύποι
- Επόμενη σελίδα JS ανάθεση