JavaScript Atomics and() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Atomics

Definition

Atomics.and(typedArray, index, value) combines the element at index with value using the bitwise AND operator (&), stores the result, and returns the element’s old value. A bit stays 1 only if it is 1 in both numbers. That makes and() the tool for switching flags off in a bitmask (a number where each bit means yes or no), while leaving the other flags alone. The whole read-change-write is atomic, so two threads editing different flags of the same shared number cannot undo each other’s work.

Remember
Atomics.and(typedArray, index, mask)   -> old value
new value = old & mask

1 & 1 = 1    1 & 0 = 0    0 & 0 = 0

clear a flag:     Atomics.and(a, i, ~FLAG)
keep low 4 bits:  Atomics.and(a, i, 0b1111)

Syntax

Pass the array, the position, and the mask to AND with.

JavaScript
Atomics.and(typedArray, index, value)
CallWhat it does
Atomics.and(typedArray, index, value)Stores typedArray[index] & value in one atomic step and returns the value from before the change.
Atomics.and(typedArray, index, ~FLAG)The everyday use: ~FLAG has every bit set except FLAG, so only that one flag is switched off.

Available since ES2017. Its siblings are Atomics.or() (switch flags on) and Atomics.xor() (flip flags).

Library declarations
JavaScript
Atomics.and(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), on a SharedArrayBuffer or a normal ArrayBuffer. Float arrays and Uint8ClampedArray throw a TypeError.
indexRequired. The element holding the bits. Out-of-range positions throw a RangeError.
valueRequired. The mask. Bits that are 0 in the mask are cleared; bits that are 1 are kept. It is converted to an integer (a BigInt for the BigInt arrays).

Return Value

Return ValueDescription
Number or BigIntThe element’s bits before the AND. Compare it with the flag to learn whether the flag was set before you cleared it.

JavaScript Examples

Example 1 — Remove the WRITE Permission

Three permissions share one byte: READ = 1, WRITE = 2, EXEC = 4. AND with ~WRITE removes only WRITE.

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

console.log(Atomics.and(perms, 0, ~WRITE));   // 7
console.log(Atomics.load(perms, 0));          // 5
console.log((perms[0] & WRITE) !== 0);        // false
console.log((perms[0] & READ) !== 0);         // true
Try It Yourself

How It Works

  1. All three flags start on. 1 + 2 + 4 is 7, which is 0b111 in binary: every permission bit is 1.
  2. The inverted mask spares the rest. ~WRITE has every bit set except bit 2, so the AND clears WRITE only. The call returns the old 7.
  3. Test single flags with &. The byte is now 5 (READ + EXEC). WRITE reads as false and READ is still true.

Example 2 — Keep Only the Low Four Bits

A status byte packs a 4-bit error code into its low bits and other data into its high bits. AND with 0b1111 keeps just the code.

JavaScript
const status = new Uint8Array(1);
status[0] = 0b10110110;                          // 182

const old = Atomics.and(status, 0, 0b00001111);
console.log(old);                                // 182
console.log(status[0]);                          // 6
console.log(status[0].toString(2).padStart(8, "0"));  // 00000110
Try It Yourself

How It Works

  1. Write the byte in binary. 0b10110110 is 182. The low four bits, 0110, hold the value we want.
  2. Zeros in the mask erase bits. The mask 0b00001111 clears the high four bits and keeps the low four, all in one atomic step.
  3. Print the result two ways. The byte is now 6, shown with padStart(8, "0") as 00000110.

Example 3 — Masks of All Ones and All Zeros

Two extreme masks make the AND rule easy to see: -1 has every bit set, 0 has none.

JavaScript
const flags = new Int32Array(2);
flags[0] = 0b1011;     // 11
flags[1] = 0b1011;     // 11

console.log(Atomics.and(flags, 0, -1));   // 11
console.log(flags[0]);                    // 11
console.log(Atomics.and(flags, 1, 0));    // 11
console.log(flags[1]);                    // 0
Try It Yourself

How It Works

  1. Two identical slots. Both elements start as 0b1011, which is 11.
  2. All ones changes nothing. In 32-bit integers -1 is every bit set to 1, so x & -1 is just x. The slot stays 11.
  3. All zeros clears everything. x & 0 is always 0. Both calls still return the old value, 11.

Browser & Runtime Support

Atomics.and() has been part of JavaScript since ES2017.

✓ Baseline · ES2017

Atomics.and()

Works in every modern browser and in Node.js, Deno, and Bun, on both normal and shared 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.and() Excellent

Bottom line: Safe to use on normal typed arrays anywhere. Sharing a flag word between browser workers additionally requires a cross-origin isolated page for SharedArrayBuffer.

Frequently Asked Questions

It replaces the element with element AND value, bit by bit, in one atomic step, and returns the old element value. A bit survives only when it is 1 in both numbers.
AND with the inverted flag: Atomics.and(flags, 0, ~FLAG). The tilde flips every bit, so the mask has a 0 only where FLAG is and the other flags stay untouched.
That is a read followed by a separate write. On shared memory, another thread could set a different flag in between and your write would erase it. Atomics.and() leaves no gap.
Check the returned old value: (Atomics.and(flags, 0, ~FLAG) & FLAG) !== 0 is true when the flag was on before you cleared it.
Yes. With BigInt64Array or BigUint64Array, pass a BigInt mask such as ~(1n << 40n). Mixing in a Number throws a TypeError.

Next: Atomics.compareExchange()

Claim a lock only when nobody else holds it, with a compare-and-swap.

compareExchange() 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