JavaScript Atomics store() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Atomics

Definition

Atomics.store(typedArray, index, value) writes value into the element at index as one atomic step and returns the value it wrote. Every thread that later reads the slot with Atomics.load() sees either the old value or your complete new value, never a half-written mix. Atomic writes are also ordered: writes you made before the store are visible to a thread that sees the store. That is why the usual pattern is “write the data first, then store the ready flag last”.

Remember
Atomics.store(typedArray, index, value)   -> value (as an integer)

Atomics.store(nums, 0, 7)     -> 7, nums[0] is 7
Atomics.store(bytes, 0, 300)  -> 300, bytes[0] is 44
Atomics.store(bytes, 0, 3.7)  -> 3,   bytes[0] is 3

write data first, store the flag last

Syntax

store() takes the array, the position, and the value to write.

JavaScript
Atomics.store(typedArray, index, value)
CallWhat it does
Atomics.store(typedArray, index, value)Writes value into typedArray[index] in one atomic step and returns the value as an integer (before any wrap-around).
Atomics.store(bigArray, index, 5n)For BigInt64Array and BigUint64Array the value must be a BigInt, and the BigInt is returned.

Available since ES2017. Unlike the other writing methods, store() does not return the old value; use Atomics.exchange() if you need it.

Library declarations
JavaScript
Atomics.store(typedArray, index, value)   // returns the value (integer)

// typedArray: integer typed arrays only (Int8 ... BigUint64)
// value:      converted to an integer; the element keeps the
//             wrapped result, the return value is not wrapped

Parameters

ParameterDescription
typedArrayRequired. Any integer typed array (Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, BigUint64Array), shared or not. Float arrays throw a TypeError.
indexRequired. The position to write. It must be from 0 to length - 1; otherwise a RangeError is thrown and nothing is written.
valueRequired. The number to write. Fractions are cut off toward zero (3.7 becomes 3). For the BigInt arrays it must be a BigInt.

Return Value

Return ValueDescription
Number or BigIntThe value you passed, converted to an integer. It is not wrapped to the element type, so it can differ from what the element now holds.

JavaScript Examples

Example 1 — Write a Status Code

A status slot uses 0 for idle and 2 for finished. store() writes the code and hands back what you wrote.

JavaScript
const status = new Int32Array(1);

console.log(Atomics.store(status, 0, 2));   // 2
console.log(Atomics.load(status, 0));       // 2
console.log(Atomics.store(status, 0, 0));   // 0
Try It Yourself

How It Works

  1. store() echoes the value. Writing 2 returns 2, not the old value 0.
  2. load() confirms the write. Reading the slot gives 2.
  3. Reset works the same way. Storing 0 puts the slot back to idle and returns 0.

Example 2 — What Is Returned and What Is Kept

A Uint8Array element holds 0 to 255. Watch the return value and the stored byte for three awkward inputs.

JavaScript
const bytes = new Uint8Array(3);

console.log(Atomics.store(bytes, 0, 300));   // 300
console.log(bytes[0]);                       // 44
console.log(Atomics.store(bytes, 1, 3.7));   // 3
console.log(bytes[1]);                       // 3
console.log(Atomics.store(bytes, 2, -1));    // -1
console.log(bytes[2]);                       // 255
Try It Yourself

How It Works

  1. Too big for a byte. 300 comes back as 300, but the byte keeps 300 - 256 = 44.
  2. Fractions drop their decimals. 3.7 is converted to the integer 3 before anything else, so both the return value and the byte are 3.
  3. Negative numbers wrap too. -1 is returned as -1, while the unsigned byte stores 255.

Example 3 — Write the Data, Then Raise the Flag

Slot 0 is a ready flag, slots 1 and 2 hold a point. The writer stores the data first and the flag last, so a reader never sees half a result.

JavaScript
const shared = new Int32Array(3);   // [ready, x, y]

function reader() {
  if (Atomics.load(shared, 0) === 0) return "not ready";
  return "x=" + Atomics.load(shared, 1) + " y=" + Atomics.load(shared, 2);
}

console.log(reader());            // not ready
Atomics.store(shared, 1, 10);
Atomics.store(shared, 2, 20);
Atomics.store(shared, 0, 1);      // publish last
console.log(reader());            // x=10 y=20
Try It Yourself

How It Works

  1. The reader checks the flag first. While slot 0 is 0 it returns "not ready" and does not look at the data.
  2. Data goes in before the flag. x and y are stored first, then the flag. Atomic stores keep this order for every thread that reads with load().
  3. A raised flag means complete data. Once the reader sees 1, both coordinates are guaranteed to be there: x=10 y=20.

Browser & Runtime Support

Atomics.store() arrived in ES2017 and accepts shared and normal integer typed arrays in modern engines.

✓ Baseline · ES2017

Atomics.store()

Supported in all modern browsers and in Node.js, Deno, and Bun.

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

Bottom line: Use it anywhere. Publishing data to another thread through a SharedArrayBuffer needs a cross-origin isolated page in browsers.

Frequently Asked Questions

The value you passed, converted to an integer. It does not return the old value; Atomics.exchange() does that.
The return value is not wrapped to the element type, but the stored element is. In a Uint8Array, storing 300 returns 300 while the element keeps 44.
On one thread the stored result is the same. With shared memory, Atomics.store() also guarantees ordering, so other threads that load the slot also see every write you made before it.
Write the data, then Atomics.store() a flag. The other thread reads the flag with Atomics.load(), or sleeps on it with Atomics.wait() and is woken with Atomics.notify().
Yes, into a BigInt64Array or BigUint64Array, and the value must then be a BigInt such as 10n. Other arrays need a Number.

Next: Atomics.sub()

Count down shared values with a subtraction that returns the old value.

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