JavaScript Atomics or() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Atomics

Definition

Atomics.or(typedArray, index, value) combines the element at index with value using bitwise OR (|), stores the result, and returns the element’s old value. A bit becomes 1 if it is 1 in either number, so or() is how you switch flags on without touching the flags that are already set. Because the step is atomic, several threads can each set their own flag in the same shared number and none of the flags gets lost.

Remember
Atomics.or(typedArray, index, mask)   -> old value
new value = old | mask

1 | 0 = 1    0 | 1 = 1    0 | 0 = 0

set a flag:  Atomics.or(a, i, FLAG)
setting it twice changes nothing

Syntax

Give it the array, the position, and the bits to switch on.

JavaScript
Atomics.or(typedArray, index, value)
CallWhat it does
Atomics.or(typedArray, index, value)Stores typedArray[index] | value in one atomic step and returns the old value.
Atomics.or(typedArray, index, A | B)Combine flags in the mask first to switch several of them on with a single call.

Available since ES2017. Use Atomics.and() with ~FLAG to switch a flag off again.

Library declarations
JavaScript
Atomics.or(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 through BigUint64Array), shared or not. Float arrays and Uint8ClampedArray are rejected with a TypeError.
indexRequired. The element whose bits you set. It must be a valid position, otherwise a RangeError is thrown.
valueRequired. The mask of bits to switch on. It is converted to an integer, or must be a BigInt for BigInt64Array and BigUint64Array.

Return Value

Return ValueDescription
Number or BigIntThe bits before the OR. If (old & FLAG) !== 0, the flag was already on before your call.

JavaScript Examples

Example 1 — Grant the WRITE Permission

The same READ = 1, WRITE = 2, EXEC = 4 flags as on the and() page. This time or() switches WRITE on, twice.

JavaScript
const READ = 1, WRITE = 2, EXEC = 4;
const perms = new Uint8Array(1);
perms[0] = READ;

console.log(Atomics.or(perms, 0, WRITE));   // 1
console.log(Atomics.or(perms, 0, WRITE));   // 3
console.log(Atomics.load(perms, 0));        // 3
console.log((perms[0] & EXEC) !== 0);       // false
Try It Yourself

How It Works

  1. Start with READ only. The byte holds 1, which is 0b001.
  2. OR adds the WRITE bit. The first call returns the old 1 and stores 0b011, which is 3.
  3. A second OR is a no-op. WRITE is already on, so the value stays 3 and the call returns 3. EXEC was never set, so it reads false.

Example 2 — Each Task Sets Its Own "Done" Bit

Three tasks finish in any order. Task number n sets bit 1 << n, and the job is complete when all three bits are on.

JavaScript
const done = new Int32Array(1);
const ALL_DONE = 0b111;

function finish(task) {
  const before = Atomics.or(done, 0, 1 << task);
  return "task " + task + " done, bits before: " + before.toString(2);
}

console.log(finish(2));   // task 2 done, bits before: 0
console.log(finish(0));   // task 0 done, bits before: 100
console.log(finish(1));   // task 1 done, bits before: 101
console.log(Atomics.load(done, 0) === ALL_DONE);   // true
Try It Yourself

How It Works

  1. One bit per task. 1 << task gives 1, 2, or 4, so every task owns a different bit of the same number.
  2. The old bits show progress. Each call returns the bits that were already set: nothing, then 100, then 101 in binary.
  3. Compare with the full mask. After the last task the value is 0b111, equal to ALL_DONE, no matter in which order the tasks finished.

Example 3 — More Than 32 Flags with BigUint64Array

A 32-bit element runs out of bits at flag 31. A BigUint64Array gives 64 flags, using BigInt masks.

JavaScript
const flags = new BigUint64Array(1);
const FLAG_40 = 1n << 40n;

Atomics.or(flags, 0, FLAG_40);
const before = Atomics.or(flags, 0, 1n);
console.log(String(before));                  // 1099511627776
console.log(String(flags[0]));                // 1099511627777
console.log((flags[0] & FLAG_40) !== 0n);     // true
Try It Yourself

How It Works

  1. Bit 40 needs BigInt. 1n << 40n is 1099511627776, a value a 32-bit integer cannot hold.
  2. Mask and element must match. Both calls pass BigInts. The second returns the old value, which is exactly bit 40.
  3. Check a high flag. After setting bit 0 the element is 1099511627777, and & FLAG_40 confirms bit 40 is still on.

Browser & Runtime Support

Atomics.or() is an ES2017 feature, supported on shared and normal integer typed arrays.

✓ Baseline · ES2017

Atomics.or()

Available in all modern browsers and in Node.js, Deno, and Bun. The 64-bit BigInt form needs BigInt support, which every modern engine has.

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.or() Excellent

Bottom line: Fine to use everywhere on normal typed arrays. In browsers, sharing the flags with workers needs SharedArrayBuffer, which requires a cross-origin isolated page.

Frequently Asked Questions

The element value from before the OR. The new value is old | mask, and you can compare the old value with your flag to see whether it was already on.
Combine them in the mask: Atomics.or(flags, 0, READ | WRITE). Both bits are switched on in the same atomic step.
No. OR can only turn bits on. To switch a flag off, use Atomics.and(flags, 0, ~FLAG), and to flip it use Atomics.xor().
Each worker sets its own bit with one atomic call, so two workers finishing at the same moment cannot overwrite each other. A plain read-then-write could lose one of the bits.
The extra high bits are dropped when the result is stored, just like any typed array write. Use a wider type such as Uint32Array or BigUint64Array for more flags.

Next: Atomics.pause()

Give the CPU a polite hint while a thread spins in a waiting loop.

pause() tutorial →

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