이 번역은 완료되지 않았습니다. 이 문서를 번역해 주세요.
할당 연산자는 오른쪽 피연산자의 값을 왼쪽의 피연산자에 할당합니다.
개요
기본 할당연산자는 등호(=) 입니다. 이것은 연산자의 오른쪽 피연산자의 값을 왼쪽 피연산자에 할당합니다. 즉, x = y 는 y의 값을 x에 할당힙니다. 다른 할당 연산자들은 보통 표준 연산자의 축약형인데, 다음과 같은 것들이 있습니다.
| 이름 | 단축 연산자 | 의미 |
|---|---|---|
| 할당 | x = y |
x = y |
| 덧셈 할당 | x += y |
x = x + y |
| 뺄셈 할당 | x -= y |
x = x - y |
| 곱셈 할당 | x *= y |
x = x * y |
| 나눗셈 할당 | x /= y |
x = x / y |
| 나머지 연산 할당 | x %= y |
x = x % y |
| 지수 연산 할당 | x **= y |
x = x ** y |
| 왼쪽 시프트 할당 | x <<= y |
x = x << y |
| 오른쪽 시프트 할당 | x >>= y |
x = x >> y |
| 부호없는 오른쪽 시프트 할당 | x >>>= y |
x = x >>> y |
| 비트 AND 할당 | x &= y |
x = x & y |
| 비트 XOR 할당 | x ^= y |
x = x ^ y |
| 비트 OR 할당 | x |= y |
x = x | y |
할당
단순 할당 연산자는 값을 변수에 할당합니다. 할당 연산자는 할당되는 값을 평가(evaluate)합니다. 한 개의 값을 여러개의 변수에 할당하기 위해 할당 연산자를 연속으로 사용할 수 있습니다. 다음 예를 봅시다.
문법
Operator: x = y
예
// 다음과 같은 변수를 가정한다. // x = 5 // y = 10 // z = 25 x = y // x 는 10 x = y = z // x, y, z 는 모두 25
덧셈 할당
덧셈 할당 연산자는 오른쪽에 있는 피연산자의 값을 변수에 더하고 그 결과를 변수에 할당합니다. 두개의 피연자 타입이 덧셈 할당 연산자의 행동을 결정합니다. 덧셈 또는 연결이 가능합니다. 자세한 내용은 addition operator을 참고하십시오.
문법
연산자: x += y 의미: x = x + y
예
// 다음과 같은 변수를 가정하면 // foo = "foo" // bar = 5 // baz = true // 숫자 + 숫자 -> 덧셈 bar += 2 // 7 // 부울 + 숫자 -> 덧셈 baz += 1 // 2 // 부울 + 부울 -> 덧셈 baz += false // 1 // 숫자 + 문자 -> 연결 bar += "foo" // "5foo" // 문자 + 부울 -> 연결 foo += false // "foofalse" // 문자 + 문자 -> 연결 foo += "bar" // "foobar"
뺄셈 할당
뺄셈 할당 연산자는 변수를 오른쪽 피연산자의 값을 뺀 뒤 그결과를 변수에 할당합니다. 더 자세한 내용은 subtraction operator을 참고하십시오.
문법
연산자: x -= y 의미: x = x - y
예
// 다음과 같은 변수를 가정하자 // bar = 5 bar -= 2 // 3 bar -= "foo" // NaN
곱셈 할당
곱셈 할당 연산자는 오른쪽 피연산자의 값으로 변수를 곱한다음 그 결과를 변수에 할당합니다. 더 자세한 내용은 multiplication operator을 참고하십시오.
문법
연산자: x *= y 의미: x = x * y
예
// 다음과 같은 변수를 가정한다. // bar = 5 bar *= 2 // 10 bar *= "foo" // NaN
나눗셈 할당
나눗셈 할당 연산자는 변수를 오른쪽 피연산자로 나눈다음 그 결과를 변수에 할당합니다. 더 자세한 내용은 division operator을 참고하십시오.
문법
연산자: x /= y 의미: x = x / y
예
// 다음과 같은 변수를 가정한다. // bar = 5 bar /= 2 // 2.5 bar /= "foo" // NaN bar /= 0 // Infinity
나머지 연산 할당
나머지 연산 할당은 변수를 오른쪽 피연산자로 나눈 다음 그 나머지를 변수에 할당합니다. 더 자세한 내용은 remainder operator을 참고하십시오.
문법
연산자: x %= y 의미: x = x % y
예
// 다음과 같은 변수를 가정한다. // bar = 5 bar %= 2 // 1 bar %= "foo" // NaN bar %= 0 // NaN
지수 연산 할당
지수연산 할당 연산자는 첫번째 피연산자를 두번째 피연산자 만큼 거듭제곱한 결과를 왼쪽 변수에 할당합니다.
자세한 내용은 exponentiation operator을 참고하십시오.
Syntax
연산자: x **= y 의미: x = x ** y
Examples
// Assuming the following variable // bar = 5 bar **= 2 // 25 bar **= "foo" // NaN
왼쪽 시프트 할당
왼쪽 시프트 할당 연산자는 지정한 수 만큼의 비트를 왼쪽으로 이동한 뒤 그 결과를 변수에 할당합니다. 더 자세한 내용은 left shift operator를 참고하십시오.
문법
연산자: x <<= y 의미: x = x << y
예
var bar = 5; // (00000000000000000000000000000101) bar <<= 2; // 20 (00000000000000000000000000010100)
오른쪽 시프트 할당
오른쪽 시프트 할당 연산자는 지정한 수 만큼 비트를 오른쪽으로 이동한 뒤 그 결과를 변수에 할당합니다. 자세한 내용은 right shift operator를 참고하십시오.
문법
연산자: x >>= y 의미: x = x >> y
예
var bar = 5; // (00000000000000000000000000000101) bar >>= 2; // 1 (00000000000000000000000000000001) var bar -5; // (-00000000000000000000000000000101) bar >>= 2; // -2 (-00000000000000000000000000000010)
없는 오른쪽 시프트 할당
부호 없는 오른쪽 시프트 할당 연산자는 지정된 수 만큼의 비트를 오른쪽으로 이동한 다음 그 결과를 변수에 할당합니다. 자세한 내용은 unsigned right shift operator을 참고하십시오.
문법
연산자: x >>>= y 의미: x = x >>> y
예
var bar = 5; // (00000000000000000000000000000101) bar >>>= 2; // 1 (00000000000000000000000000000001) var bar = -5; // (-00000000000000000000000000000101) bar >>>= 2; // 1073741822 (00111111111111111111111111111110)
비트 AND 할당
비트 AND 할당 연산자는 피연산자 모두 비트(바이너리)표현를 사용해서 비트 AND 연산을 한 다음 그 결과를 변수에 할당합니다. 자세한 내용은bitwise AND operator을 참고하십시오.
문법
연산자: x &= y 의미: x = x & y
예
var bar = 5; // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 bar &= 2; // 0
비트 XOR 할당
비트 XOR 할당 연산자는 피연산자 모두 비트(바이너리)표현를 사용해서 비트 XOR 연산을 한 다음 그 결과를 변수에 할당합니다. 자세한 내용은 bitwise XOR operator를 참고하십시오.
문법
연산자: x ^= y 의미: x = x ^ y
예
var bar = 5; bar ^= 2; // 7 // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 // ----------------------------------- // 7: 00000000000000000000000000000111
비트 OR 할당
비트 OR 할당 연산자는 피연산자 모두 비트(바이너리)표현를 사용해서 비트 OR연산을 한 다음 그 결과를 변수에 할당합니다. 자세한 내용은 bitwise OR operator를 참고하십시오.
문법
연산자: x |= y 의미: x = x | y
예
var bar = 5; bar |= 2; // 7 // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 // ----------------------------------- // 7: 00000000000000000000000000000111
예
다른 할당 연산자를 갖는 왼쪽 피연산자
드물게, 할당 연산자(예 x += y)가 그 의미를 나타낸 표현(x = x + y)과 동일하지 않은 경우가 있습니다. 할당 연산자의 왼쪽 피연산자 그 자신이 할당 연산자를 포함할 때, 왼쪽 피연산자는 한번만 계산됩니다. 예를 들면 다음과 같습니다.
a[i++] += 5 // i는 한 번만 계산된다. a[i++] = a[i++] + 5 // i는 두 번 계산된다.
스펙
Browser compatibility
| Desktop | Mobile | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Addition assignment (x += y) | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Bitwise AND assignment (x &= y) | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Bitwise OR assignment (x |= y) | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Bitwise XOR assignment (x ^= y) | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Division assignment (x /= y) | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Exponentiation assignment (x **= y) | Chrome Full support 52 | Edge ? | 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 | Edge Mobile ? | Firefox Android Full support 52 | Opera Android Full support Yes | Safari iOS Full support 10.1 | Samsung Internet Android Full support 6.0 | nodejs
Full support
7.0.0
|
Left shift assignment (x <<= y) | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Multiplication assignment (x *= y) | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Remainder assignment (x %= y) | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Right shift assignment (x >>= y) | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Assignment (x = y) | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Subtraction assignment (x -= y) | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Unsigned right shift assignment (x >>>= y) | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- User must explicitly enable this feature.
- User must explicitly enable this feature.