按位异或赋值 (^=)

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

按位异或赋值操作符 (^=) 使用二进制表示操作数,进行一次按位异或操作并赋值。

尝试一下

let a = 5; // 00000000000000000000000000000101
a ^= 3; // 00000000000000000000000000000011

console.log(a); // 00000000000000000000000000000110
// Expected output: 6

语法

Operator: x ^= y
Meaning:  x  = x ^ y

示例

使用按位异或赋值

js
let a = 5; // 00000000000000000000000000000101
a ^= 3; // 00000000000000000000000000000011

console.log(a); // 00000000000000000000000000000110
// 6

let b = 5; // 00000000000000000000000000000101
b ^= 0; // 00000000000000000000000000000000

console.log(b); // 00000000000000000000000000000101
// 5

规范

Specification
ECMAScript® 2025 Language Specification
# sec-assignment-operators

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
Bitwise XOR assignment (x ^= y)

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见