산술 연산자는 숫자 값(리터럴 또는 변수)을 피연산자로 받아 하나의 숫자 값을 반환합니다. 표준 산술 연산자는 덧셈(+), 뺄셈(-), 곱셈(*), 나눗셈(/)입니다.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
덧셈 (+)
덧셈 연산자는 숫자 피연산자를 더한 값, 또는 문자열을 연결한 값을 생성합니다.
구문
연산자: x + y
예제
// Number + Number -> 합 1 + 2 // 3 // Boolean + Number -> 합 true + 1 // 2 // Boolean + Boolean -> 합 false + false // 0 // Number + String -> 연결 5 + "foo" // "5foo" // String + Boolean -> 연결 "foo" + false // "foofalse" // String + String -> 연결 "foo" + "bar" // "foobar"
뺄셈 (-)
뺄셈 연산자는 두 개의 피연산자를 뺀 값을 생성합니다.
구문
연산자: x - y
예제
5 - 3 // 2 3 - 5 // -2 "foo" - 3 // NaN
나눗셈 (/)
나눗셈 연산자는 왼쪽 피연산자를 피제수로, 오른쪽 피연산자를 제수로 한 몫을 생성합니다.
구문
연산자: x / y
예제
1 / 2 // JavaScript에선 0.5 1 / 2 // Java에선 0 // (양쪽 피연산자 모두 명시적인 부동소수점 숫자가 아님) 1.0 / 2.0 // JavaScript와 Java 둘 다 0.5 2.0 / 0 // JavaScript에서 Infinity 2.0 / 0.0 // 동일하게 Infinity 2.0 / -0.0 // JavaScript에서 -Infinity
곱셈 (*)
곱셈 연산자는 두 연산자의 곱을 생성합니다.
구문
연산자: x * y
예제
2 * 2 // 4 -2 * 2 // -4 Infinity * 0 // NaN Infinity * Infinity // Infinity "foo" * 2 // NaN
나머지 (%)
나머지 연산자는 왼쪽 피연산자를 오른쪽 피연산자로 나눴을 때의 나머지를 반환합니다. 결과는 항상 피제수의 부호를 따라갑니다.
구문
연산자: var1 % var2
예제
12 % 5 // 2 -1 % 2 // -1 NaN % 2 // NaN 1 % 2 // 1 2 % 3 // 2 -4 % 2 // -0 5.5 % 2 // 1.5
거듭제곱 (**)
거듭제곱 연산자는 첫 번째 피연산자를 밑으로, 두 번째 피연산자를 지수로 한 값을 생성합니다. 즉, var1과 var2가 변수일 때, var1var2의 값을 생성합니다. 거듭제곱 연산자는 우결합성을 가집니다. 따라서 a ** b ** c는 a ** (b ** c)와 같습니다.
구문
연산자: var1 ** var2
참고
PHP와 Python 등 거듭제곱 연산자를 가진 대부분의 언어는 거듭제곱 연산자의 우선순위가 +와 - 등 단항 연산자보다 높습니다. 그러나 Bash는 단항 연산자가 거듭제곱 연산자보다 우선순위가 높은 등 일부 예외도 있습니다. JavaScript는 단항 연산자(+/-/~/!/delete/void/typeof)를 왼쪽 피연산자 앞에 배치할 수 없으므로, 모호한 표현도 작성할 수 없습니다.
-2 ** 2; // Bash에서 4, 다른 언어에서는 -4. // 모호한 표현이므로 JavaScript에서는 유효하지 않음 -(2 ** 2); // JavaScript에서 -4, 작성자의 의도가 명확함
예제
2 ** 3 // 8 3 ** 2 // 9 3 ** 2.5 // 15.588457268119896 10 ** -1 // 0.1 NaN ** 2 // NaN 2 ** 3 ** 2 // 512 2 ** (3 ** 2) // 512 (2 ** 3) ** 2 // 64
결과의 부호를 뒤집으려면 다음과 같이 작성합니다.
-(2 ** 2) // -4
거듭제곱의 밑을 음수로 강제하려면 다음과 같이 작성합니다.
(-2) ** 2 // 4
참고: JavaScript는 비트 연산자 ^ (논리 XOR)도 가지고 있습니다. **와 ^는 다릅니다. (예 : 2 ** 3 === 8이지만 2 ^ 3 === 1)
증가 (++)
증가 연산자는 피연산자를 증가(1을 덧셈)시키고, 그 값을 반환합니다.
- 피연산자 뒤에 붙여(예:
x++) 접미사로 사용한 경우 증가하기 전의 값을 반환합니다. - 피연산자 앞에 붙여(예:
++x) 접두사로 사용한 경우 증가한 후의 값을 반환합니다.
구문
연산자: x++ or ++x
예제
// 접미사 var x = 3; y = x++; // y = 3, x = 4 // 접두사 var a = 2; b = ++a; // a = 3, b = 3
감소 (--)
감소 연산자는 피연산자를 감소(1을 뺄셈)시키고, 그 값을 반환합니다.
- 피연산자 뒤에 붙여(예:
x--) 접미사로 사용한 경우 감소하기 전의 값을 반환합니다. - 피연산자 앞에 붙여(예:
--x) 접두사로 사용한 경우 감소한 후의 값을 반환합니다.
구문
연산자: x-- or --x
예제
// 접미사 var x = 3; y = x--; // y = 3, x = 2 // 접두사 var a = 2; b = --a; // a = 1, b = 1
단항 부정 (-)
단항 부정 연산자는 피연산자의 앞에 위치하며 부호를 뒤집습니다.
구문
연산자: -x
예제
var x = 3; y = -x; // y = -3, x = 3 // 단항 부정 연산자는 숫자가 아닌 값을 숫자로 변환함 var x = "4"; y = -x; // y = -4
단항 양부호 (+)
단항 양부호 연산자는 피연산자의 앞에 위치하며 피연산자의 값 그대로 평가되지만, 값이 숫자가 아닐 경우 숫자로 변환을 시도합니다. 단항 부정(-) 연산자도 숫자가 아닌 값을 변환할 수 있지만, 단항 양부호가 속도도 제일 빠르고 다른 연산도 수행하지 않으므로 선호해야 할 방법입니다. 문자열로 표현한 정수 및 부동소수점 실수, 문자열이 아닌 true, false, null도 변환할 수 있습니다. 10진수와 (앞에 "0x"가 붙은) 16진수 정수 모두 지원합니다. 음수도 지원하지만 16진수 음수에는 사용할 수 없습니다. 어떤 값을 분석할 수 없으면 NaN으로 평가됩니다.
구문
연산자: +x
예제
+3 // 3
+"3" // 3
+true // 1
+false // 0
+null // 0
+function(val) { return val } // NaN
명세
브라우저 호환성
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Addition (+) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Decrement (--) | Chrome Full support 2 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support 4 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 3.2 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Division (/) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Exponentiation (**) | Chrome Full support 52 | Edge Full support 14 | Firefox Full support 52 | IE No support No | Opera Full support Yes | Safari Full support 10.1 | WebView Android Full support 51 | Chrome Android Full support 52 | Firefox Android Full support 52 | Opera Android Full support Yes | Safari iOS Full support 10.3 | Samsung Internet Android Full support 6.0 | nodejs
Full support
7.0.0
|
Increment (++) | Chrome Full support 2 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support 4 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 3.2 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Multiplication (*) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Remainder (%) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Subtraction (-) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Unary negation (-) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Unary plus (+) | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- User must explicitly enable this feature.
- User must explicitly enable this feature.