JavaScript Atomics Methods

Beginner
4 min read
Updated: Nov 2026
14 method guides
Method index

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

All methods are static.

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:

JavaScript
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 methods

Read or replace one element of an integer typed array as a single, uninterruptible step.

MethodDescriptionTutorial
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 methods

Change an element with + or - or a bitwise AND / OR / XOR. Each one returns the value from before the change.

MethodDescriptionTutorial
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 methods

Let one thread sleep until another thread changes a value, or hint the CPU while you spin.

MethodDescriptionTutorial
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 method

Ask the engine how it implements atomic operations of a given size.

MethodDescriptionTutorial
Atomics.isLockFree()Ask whether atomic operations on elements of a given byte size are lock-free on this engine.Open

Frequently Asked Questions

Fourteen: add, sub, and, or, xor, load, store, exchange, compareExchange, isLockFree, wait, waitAsync, notify, and pause. All of them are called on the Atomics object itself.
Start with Atomics.add() and Atomics.load(). Together they build a counter that never loses an update, which is the classic reason Atomics exists.
Most of them do: the read, write, math, and bit methods accept a typed array on a normal ArrayBuffer. Only Atomics.wait() and Atomics.waitAsync() require shared memory.
add, sub, and, or, xor, exchange, and compareExchange all return the value the element had before the change. store returns the value it wrote, and load returns the current value.
Atomics.waitAsync() was added in ES2024 and Atomics.pause() is newer still, so feature-detect both with typeof before you use them.

Start with Atomics.add()

Build a counter that never loses an update, and see why add() returns the old value.

add() 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.

14 people found this page helpful