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

The Atomics.exchange() static method stores 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 between the read of the old value and the write of the new value.

Syntax

Atomics.exchange(typedArray, index, value)

Parameters

Return value

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

Exceptions

Examples

Using exchange()

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

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

Specifications

Browser compatibility

See also