Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
** | Exponentiation (ES2016) |
/ | Division |
% | Modulus (Remainder) |
++ | Increment |
-- | Decrement |
일반적인 산술 연산은 두개의 숫자에서 작동한다.
그 두개의 숫자는 고정된 값일 수 있다.(상수)
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>A typical arithmetic operation takes two numbers and produces a new number.</p>
<p id="demo"></p>
<script>
let x = 100 + 50;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>A typical arithmetic operation takes two numbers (or variables) and produces a new number.</p>
<p id="demo"></p>
<script>
let a = 100;
let b = 50;
let x = a + b;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>A typical arithmetic operation takes two numbers (or expressions) and produces a new number.</p>
<p id="demo"></p>
<script>
let a = 3;
let x = (100 + 50) * a;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Operand | Operator | Operand |
---|---|---|
100 | + | 50 |
덧셈 연산자 +
는 숫자를 더한다.
let x = 5;
let y = 2;
let z = x + y;
뺄셈 연산자 -
는 숫자를 뺀다.
let x = 5;
let y = 2;
let z = x - y;
곱셈 연산자 *
는 숫자를 곱한다.
let x = 5;
let y = 2;
let z = x * y;
나눗셈 연산자 /
는 숫자를 나눈다.
let x = 5;
let y = 2;
let z = x / y;
계수 연산자 %
는 숫자를 나눈 후 나머지 값을 리턴한다.
let x = 5;
let y = 2;
let z = x % y;
증가 연산자는 ++
숫자를 증가시킨다.
let x = 5;
x++;
let z = x;
감소 연산자는 --
숫자를 감소시킨다.
let x = 5;
x--;
let z = x;
제곱(지수) 연산자 **
는 첫 번째 피연산자를 두 번째 피연산자의 거듭제곱으로 올린다.
let x = 5;
let z = x ** 2; // result is 25
Math.pow(x,y)
:let x = 5;
let z = Math.pow(x,2); // result is 25
연산자 우선순위는 산술식에서 연산이 수행되는 순서를 설명한다.
아래 예제의 결과는 100 + 150이다. 일반적인 수학과 동일하게 곱셈부터 진행한다.
역시 ( )괄호를 이용하여 우선순위를 지정할 수 있다.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>Multiplication has precedence over addition.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 100 + 50 * 3;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>Multiplication has precedence over addition.</p>
<p>But parenthesis has precedence over multiplication.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = (100 + 50) * 3;
</script>
</body>
</html>
Value | Operator | Description | Example |
---|---|---|---|
21 | ( ) | Expression grouping | (3 + 4) |
20 | . | Member | person.name |
20 | [] | Member person | ["name"] |
20 | () | Function call | myFunction() |
20 | new | Create | new Date() |
18 | ++ | Postfix Increment | i++ |
18 | -- | Postfix Decrement | i-- |
17 | ++ | Prefix Increment | ++i |
17 | -- | Prefix Decrement | --i |
17 | ! | Logical not | !(x==y) |
17 | typeof | Type | typeof x |
16 | ** | Exponentiation(ES2016) | 10 ** 2 |
15 | * | Multiplication | 10 * 5 |
15 | / | Division | 10 / 5 |
15 | % | Division Remainder | 10 % 5 |
14 | + | Addition | 10 + 5 |
14 | - | Subtraction | 10 - 5 |
13 | << | Shift left | x << 2 |
13 | >> | Shift right | x >> 2 |
13 | >>> | Shift right (unsigned) | x >>> 2 |
12 | < | Less than | x < y |
12 | <= | Less than or equal | x <= y |
12 | > | Greater than | x > y |
12 | >= | Greater than or equal | x >= y |
12 | in | Property in Object | "PI" in Math |
12 | instanceof | Instance of Object | instanceof Array |
11 | == | Equal | x == y |
11 | === | Strict equal | x === y |
11 | != | Unequal | x != y |
11 | !== | Strict unequal | x !== y |
10 | & | Bitwise AND | x & y |
9 | ^ | Bitwise XOR | x ^ y |
8 | | | Bitwise OR | x |
7 | && | Logical AND | x && y |
6 | || | Logical OR | x |
5 | ?? | Nullish Coalescing | x ?? y |
4 | ? : | Condition | ? "Yes" : "No" |
3 | += | Assignment | x += y |
3 | /= | Assignment | x /= y |
3 | -= | Assignment | x -= y |
3 | *= | Assignment | x *= y |
3 | %= | Assignment | x %= y |
3 | <<= | Assignment | x <<= y |
3 | >>= | Assignment | x >>= y |
3 | >>>= | Assignment | x >>>= y |
3 | &= | Assignment | x &= y |
3 | ^= | Assignment | x ^= y |
3 | |= | Assignment | x |
2 | yield | Pause Function | yield x |
1 | , | Comma | 5 , 6 |