Bản dịch này chưa hoàn thành. Xin hãy giúp dịch bài viết này từ tiếng Anh.
?Toán tử gán gán giá trị cho toán hạng bên trái nó dựa theo giá trị của toán hạng bên phải nó.
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.
Sơ lược
Toán tử gán căn bản là bằng (=
), gán giá trị cho toán hạng bên trái bằng giá trị của toán hạng bên phải. Tức là, x = y
gán giá trị của y
cho x
. Các toán tử gán khác thường là viết tắt cho các toán tử thông thường, được định nghĩa và lấy ví dụ như bảng sau.
Tên | Toán tử viết tắt | Ý nghĩa |
---|---|---|
Gán | x = y |
x = y |
Cộng xong rồi gán | x += y |
x = x + y |
Trừ xong rồi gán | x -= y |
x = x - y |
Nhân xong rồi gán | x *= y |
x = x * y |
Chia xong rồi gán | x /= y |
x = x / y |
Chia lấy dư xong rồi gán | x %= y |
x = x % y |
?Luỹ thừa xong rồi gán | x **= y |
x = x ** y |
Dịch bit sang trái rồi gán | x <<= y |
x = x << y |
Dịch bit sang phải rồi gán | x >>= y |
x = x >> y |
Unsigned right shift assignment | x >>>= y |
x = x >>> y |
Bitwise AND assignment | x &= y |
x = x & y |
Bitwise XOR assignment | x ^= y |
x = x ^ y |
Bitwise OR assignment | x |= y |
x = x | y |
Gán
Simple assignment operator which assigns a value to a variable. The assignment operation evaluates to the assigned value. Chaining the assignment operator is possible in order to assign a single value to multiple variables. See the example.
Cú pháp
Toán tử: x = y
Ví dụ
// Giả dụ có 3 biến x, y, z như sau // x = 5 // y = 10 // z = 25 x = y // x bằng 10 x = y = z // x, y và z đều bằng 25
Cộng xong rồi gán
Toán tử cộng xong rồi gán ?thêm giá trị của toán hạng bên phải cho một biến và gán kết quả cho chính biến đó. Kiểu dữ liệu của hai toán hạng sẽ xác định hành vi của toán tử cộng xong rồi gán. Cộng hay nối chuỗi đều được. ?Tìm đọc addition operator để xem chi tiết.
Cú pháp
Toán tử: x += y Ý nghĩa: x = x + y
?
Ví dụ
// Giả dụ ta có các biến sau // foo = 'foo' // bar = 5 // baz = true // Số + Số -> cộng bar += 2 // 7 // Boolean + Số -> cộng baz += 1 // 2 // Boolean + Boolean -> cộng baz += false // 1 // Số + Chuỗi -> nối chuỗi bar += 'foo' // "5foo" // Chuỗi + Boolean -> nối chuỗi foo += false // "foofalse" // Chuỗi + Chuỗi -> nối chuỗi foo += 'bar' // "foobar"
Trừ xong rồi gán
Toán từ trừ xong rồi gán ?bớt giá trị của toán hạng bên phải khỏi một biến và gán kết quả vào chính biến đó. Tìm đọc subtraction operator ?để biết thêm chi tiết.
Cú pháp
Toán tử: x -= y Ý nghĩa: x = x - y
Ví dụ
// Giả dụ ta có biến sau // bar = 5 bar -= 2 // 3 bar -= 'foo' // NaN
Nhân xong rồi gán
Toán tử nhân xong rồi gán nhân một biến với giá trị của toán hạng bên phải và gán kết quả vào biến đó. Tìm đọc multiplication operator để biết thêm chi tiết.
Cú pháp
Toán tử: x *= y Ý nghĩa: x = x * y
Ví dụ
// Giả dụ ta có biến sau // bar = 5 bar *= 2 // 10 bar *= 'foo' // NaN
Chia xong rồi gán
Toán tử chia xong rồi gán chia một biến bởi giá trị của toán hạng bên phải và gán kết quả vào chính biến đó. Tìm đọc division operator để biết thêm chi tiết.
Cú pháp
Toán tử: x /= y Ý nghĩa: x = x / y
Ví dụ
// Giả dụ ta có biến sau // bar = 5 bar /= 2 // 2.5 bar /= 'foo' // NaN bar /= 0 // Infinity
Chia lấy dư xong rồi gán
Toán tử chia lấy dư xong rồi gán chia một biến bởi giá trị của toán hạng bên phải và gán phần dư vào chính biến đó. Tìm đọc remainder operator để biết thêm chi tiết.
Cú pháp
Toán tử: x %= y Ý nghĩa: x = x % y
Ví dụ
// Giả dụ ta có biến sau // bar = 5 bar %= 2 // 1 bar %= 'foo' // NaN bar %= 0 // NaN
Luỹ thừa xong rồi gán
Toán tử luỹ thừa xong rồi gán tạo ra kết quả bằng cách nâng toán hạng ?thứ nhất lên với bậc là toán hạng thứ hai rồi gán kết quả vào toán hạng thứ nhất. Tìm đọc exponentiation operator để biết thêm chi tiết.
Cú pháp
Toán tử: x **= y Ý nghĩa: x = x ** y
Ví dụ
// Giả dụ ta có biến sau // bar = 5 bar **= 2 // 25 bar **= 'foo' // NaN
Dịch bit sang trái rồi gán
Toán tử dịch trái rồi gán di chuyển số bit của toán hạng thứ nhất sang trái dựa theo giá trị của toán hạng bên phải và gán kết quả vào toán hạng bên trái. Tìm đọc left shift operator để biết thêm chi tiết.
Cú pháp
Toán tử: x <<= y Ý nghĩa: x = x << y
Ví dụ
var bar = 5; // (00000000000000000000000000000101) bar <<= 2; // 20 (00000000000000000000000000010100)
Dịch phải rồi gán
Toán tử dịch trái rồi gán di chuyển số bit của toán hạng thứ nhất sang phải dựa theo giá trị của toán hạng bên phải và gán kết quả vào toán hạng bên trái. Tìm đọc right shift operator để biết thêm chi tiết.
Cú pháp
Toán tử: x >>= y Ý nghĩa: x = x >> y
Ví dụ
var bar = 5; // (00000000000000000000000000000101) bar >>= 2; // 1 (00000000000000000000000000000001) var bar -5; // (-00000000000000000000000000000101) bar >>= 2; // -2 (-00000000000000000000000000000010)
Unsigned right shift assignment
The unsigned right shift assignment operator moves the specified amount of bits to the right and assigns the result to the variable. See the unsigned right shift operator for more details.
Syntax
Operator: x >>>= y Meaning: x = x >>> y
Examples
var bar = 5; // (00000000000000000000000000000101) bar >>>= 2; // 1 (00000000000000000000000000000001) var bar = -5; // (-00000000000000000000000000000101) bar >>>= 2; // 1073741822 (00111111111111111111111111111110)
Bitwise AND assignment
The bitwise AND assignment operator uses the binary representation of both operands, does a bitwise AND operation on them and assigns the result to the variable. See the bitwise AND operator for more details.
Syntax
Operator: x &= y Meaning: x = x & y
Example
var bar = 5; // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 bar &= 2; // 0
Bitwise XOR assignment
The bitwise XOR assignment operator uses the binary representation of both operands, does a bitwise XOR operation on them and assigns the result to the variable. See the bitwise XOR operator for more details.
Syntax
Operator: x ^= y Meaning: x = x ^ y
Example
var bar = 5; bar ^= 2; // 7 // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 // ----------------------------------- // 7: 00000000000000000000000000000111
Bitwise OR assignment
The bitwise OR assignment operator uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable. See the bitwise OR operator for more details.
Syntax
Operator: x |= y Meaning: x = x | y
Example
var bar = 5; bar |= 2; // 7 // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 // ----------------------------------- // 7: 00000000000000000000000000000111
Examples
Left operand with another assignment operator
In unusual situations, the assignment operator (e.g. x += y
) is not identical to the meaning expression (here x = x + y
). When the left operand of an assignment operator itself contains an assignment operator, the left operand is evaluated only once. For example:
a[i++] += 5 // i is evaluated only once a[i++] = a[i++] + 5 // i is evaluated twice
Specifications
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.