Atomics.xor(typedArray, index, value) combines the element at index with value using bitwise XOR (^, “exclusive or”), stores the result, and returns the element’s old value. Every bit that is 1 in the mask is flipped: 0 becomes 1 and 1 becomes 0. Bits that are 0 in the mask stay as they are. That makes xor() the natural way to toggle a flag, and doing the same XOR twice brings back the original value. The step is atomic, so it is safe on memory shared between threads.
Remember
Atomics.xor(typedArray, index, mask) -> old value
new value = old ^ mask
1 ^ 1 = 0 1 ^ 0 = 1 0 ^ 0 = 0
toggle a flag: Atomics.xor(a, i, FLAG)
XOR twice with the same mask -> original value
Syntax
Pass the array, the position, and the bits to flip.
JavaScript
Atomics.xor(typedArray, index, value)
Call
What it does
Atomics.xor(typedArray, index, value)
Stores typedArray[index] ^ value as one atomic step and returns the value from before.
Atomics.xor(typedArray, index, FLAG)
Toggles a single flag: on becomes off and off becomes on. Check the returned old value to know which way it went.
Part of JavaScript since ES2017, like Atomics.and() and Atomics.or(). Use those when you need a flag to end up definitely off or definitely on.
Library declarations
JavaScript
Atomics.xor(typedArray, index, value) // returns the old element value
// stores: typedArray[index] ^ value
// typedArray: integer typed arrays only (Int8 ... BigUint64)
// value: Number, or BigInt for the BigInt arrays
Parameters
Parameter
Description
typedArray
Required. An integer typed array (Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, BigUint64Array), shared or not. Float arrays throw a TypeError.
index
Required. The element whose bits you flip. Invalid positions throw a RangeError.
value
Required. The mask: each 1 bit flips the matching bit of the element. It is converted to an integer, or must be a BigInt for the BigInt arrays.
Return Value
Return Value
Description
Number or BigInt
The element’s value before the XOR. (old & FLAG) !== 0 tells you the flag was on and is now off.
JavaScript Examples
Example 1 — Toggle a Light Switch
Bit 0 stands for a light. XOR with 1 turns it on, and the next XOR with 1 turns it off again.
Line up the bits. Value 0110 and mask 0101: the mask has 1s at positions 0 and 2.
Only masked bits change. Bit 0 goes from 0 to 1, bit 2 goes from 1 to 0, and bits 1 and 3 are untouched.
The result is 0011. The call returns the old 0110; the element now reads 0011.
Example 3 — XOR Twice Brings the Data Back
XOR every byte of a short message with a key, then do it again. The second pass restores the original text.
JavaScript
const message = new Uint8Array([72, 105]); // "Hi"
const KEY = 42;
for (let i = 0; i < message.length; i++) Atomics.xor(message, i, KEY);
console.log(Array.from(message).join(", ")); // 98, 67
for (let i = 0; i < message.length; i++) Atomics.xor(message, i, KEY);
console.log(String.fromCharCode(message[0], message[1])); // Hi
Bytes for "Hi". Character codes 72 and 105 are stored in a Uint8Array.
The first pass scrambles them. 72 ^ 42 is 98 and 105 ^ 42 is 67, so the bytes no longer spell "Hi".
The second pass undoes it. Because (x ^ k) ^ k equals x, the same loop restores 72 and 105. This is a toy, not real encryption.
Compatibility
Browser & Runtime Support
Atomics.xor() shipped with the rest of the Atomics object in ES2017.
✓ Baseline · ES2017
Atomics.xor()
Supported in all modern browsers and in Node.js, Deno, and Bun, on shared and normal integer typed arrays.
95%Modern browser support
Google ChromeModern versions · Desktop & Mobile
Full support
Mozilla FirefoxModern versions · Desktop & Mobile
Full support
Apple SafariModern versions · macOS & iOS
Full support
Microsoft EdgeModern versions · Chromium
Full support
OperaModern versions
Full support
Samsung InternetModern versions · Android
Full support
Internet ExplorerNo Atomics object
Not supported
Node.jsCurrent releases · checked on Node 24
Supported
DenoCurrent releases
Supported
BunCurrent releases
Supported
Atomics.xor()Excellent
Bottom line: Works anywhere on normal typed arrays. Shared flags between browser workers need SharedArrayBuffer, which only cross-origin isolated pages get.
Frequently Asked Questions
It flips every bit of the element that is 1 in the mask, stores the result in one atomic step, and returns the old value.
Call Atomics.xor(flags, 0, FLAG). If the flag was off it turns on, and if it was on it turns off. The returned old value tells you which happened.
Flipping the same bits twice puts them back where they were: (x ^ mask) ^ mask is always x.
Use or() when a flag must end up on and and() with ~FLAG when it must end up off. xor() depends on the current state, so two toggles cancel each other out.
Yes. Like most Atomics methods it accepts integer typed arrays over a normal ArrayBuffer as well as over a SharedArrayBuffer.