UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ Atomics/ Atomics.add()

The Atomics.add() static method adds a given value at a given position in the array and returns the old value at that position. This atomic operation guarantees that no other write happens until the modified value is written back.

Syntax

Atomics.add(typedArray, index, value)

Parameters

Return value

The old value at the given position (typedArray[index]).

Exceptions

Examples

Using add()

const sab = new SharedArrayBuffer(1024);
const ta = new Uint8Array(sab);

Atomics.add(ta, 0, 12); // returns 0, the old value
Atomics.load(ta, 0); // 12

Specifications

Browser compatibility

See also