JavaScript Atomics Methods

About This Index
This page is the JavaScript Atomics Methods index for the tutorial track. Each row links to a full guide with syntax, three runnable examples, browser notes, and FAQs.
Learn what Atomics is and why it exists on /js/atomic. Use this page when you need one specific method, such as Atomics.add(), Atomics.compareExchange(), or Atomics.wait().
Atomics is a plain built-in object, like Math. You never write new Atomics(); you call Atomics.add(...), Atomics.load(...), and so on, always passing an integer typed array and an index.
How to Call an Atomics Method
Every method takes the typed array first and the index second. Most of them also take a value:
const shared = new Int32Array(new SharedArrayBuffer(16)); // 4 slots
Atomics.store(shared, 0, 5); // write 5 into slot 0
Atomics.add(shared, 0, 2); // returns 5, slot 0 is now 7
Atomics.load(shared, 0); // 7
Atomics.compareExchange(shared, 0, 7, 0); // returns 7, slot 0 is now 0 JavaScript Atomics Methods Table
All method guides in this JavaScript tutorial series (14 tutorials), grouped by what they do.
Read and write
4 methodsRead or replace one element of an integer typed array as a single, uninterruptible step.
| Method | Description | Tutorial |
|---|---|---|
Atomics.load() | Read one element as a single atomic step, so you always see a complete, up-to-date value. | Open |
Atomics.store() | Write one element as a single atomic step; returns the value you passed (converted to an integer). | Open |
Atomics.exchange() | Write a new value and get the previous one back in the same atomic step (an atomic swap). | Open |
Atomics.compareExchange() | Write a new value only if the element still holds the expected value. Always returns the old value. | Open |
Math and bit operations
5 methodsChange an element with + or - or a bitwise AND / OR / XOR. Each one returns the value from before the change.
| Method | Description | Tutorial |
|---|---|---|
Atomics.add() | Add a number to one element in a single safe step and get the value it had before. | Open |
Atomics.sub() | Subtract a number from one element in a single safe step and get the value it had before. | Open |
Atomics.and() | Bitwise AND an element with a mask in one safe step, typically to switch flags off. | Open |
Atomics.or() | Bitwise OR an element with a mask in one safe step, typically to switch flags on. | Open |
Atomics.xor() | Bitwise XOR an element with a mask in one safe step, which flips (toggles) the chosen bits. | Open |
Waiting and waking
4 methodsLet one thread sleep until another thread changes a value, or hint the CPU while you spin.
| Method | Description | Tutorial |
|---|---|---|
Atomics.wait() | Put the current thread to sleep while a shared element still holds a given value, until it is notified or times out. | Open |
Atomics.waitAsync() | Wait for a shared element to be notified without blocking: returns an object whose value is a string or a promise. | Open |
Atomics.notify() | Wake threads waiting on an element and return how many were woken (0 if nobody was waiting). | Open |
Atomics.pause() | Tell the CPU that this thread is spinning in a wait loop. Returns undefined and changes no memory. | Open |
Helper
1 methodAsk the engine how it implements atomic operations of a given size.
| Method | Description | Tutorial |
|---|---|---|
Atomics.isLockFree() | Ask whether atomic operations on elements of a given byte size are lock-free on this engine. | Open |
Frequently Asked Questions
Start with Atomics.add()
Build a counter that never loses an update, and see why add() returns the old value.
14 people found this page helpful
