JavaScript Atomics xor() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Atomics

Definition

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)
CallWhat 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

ParameterDescription
typedArrayRequired. An integer typed array (Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, BigUint64Array), shared or not. Float arrays throw a TypeError.
indexRequired. The element whose bits you flip. Invalid positions throw a RangeError.
valueRequired. 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 ValueDescription
Number or BigIntThe 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.

JavaScript
const LIGHT = 1;
const state = new Uint8Array(1);

console.log(Atomics.xor(state, 0, LIGHT));   // 0
console.log(state[0]);                       // 1
console.log(Atomics.xor(state, 0, LIGHT));   // 1
console.log(state[0]);                       // 0
Try It Yourself

How It Works

  1. The light starts off. A new Uint8Array is all zeros, so bit 0 is off.
  2. The first XOR flips it on. It returns the old 0 and the element becomes 1.
  3. The same call flips it back. XOR with 1 again returns 1 and the light is off, value 0.

Example 2 — Flip Several Bits in One Call

A mask with more than one 1 bit flips all of those bits together and leaves the others alone.

JavaScript
const bits = new Uint8Array(1);
bits[0] = 0b0110;

const old = Atomics.xor(bits, 0, 0b0101);
console.log(old.toString(2).padStart(4, "0"));       // 0110
console.log(bits[0].toString(2).padStart(4, "0"));   // 0011
Try It Yourself

How It Works

  1. Line up the bits. Value 0110 and mask 0101: the mask has 1s at positions 0 and 2.
  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.
  3. 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
Try It Yourself

How It Works

  1. Bytes for "Hi". Character codes 72 and 105 are stored in a Uint8Array.
  2. The first pass scrambles them. 72 ^ 42 is 98 and 105 ^ 42 is 67, so the bytes no longer spell "Hi".
  3. 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.

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 Chrome Modern versions · Desktop & Mobile
Full support
Mozilla Firefox Modern versions · Desktop & Mobile
Full support
Apple Safari Modern versions · macOS & iOS
Full support
Microsoft Edge Modern versions · Chromium
Full support
Opera Modern versions
Full support
Samsung Internet Modern versions · Android
Full support
Internet Explorer No Atomics object
Not supported
Node.js Current releases · checked on Node 24
Supported
Deno Current releases
Supported
Bun Current 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.

Back to the Atomics Method Index

You have reached the last Atomics method. Pick your next topic from the index.

Atomics methods →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful