Standard built-ins

Atomics.exchange()

Try the free JavaScript compiler

Replaces the element and returns its previous value. Use a shared buffer for communication between agents; the example checks operation semantics in Node.js, not inter-thread ordering.

Formal Syntax & Structure

javascript syntaxjavascript
Atomics.exchange(typedArray, index, value)

Parameters & Attributes

NameTypeDescription
typedArrayinteger TypedArrayInteger view; floating-point and clamped arrays are not accepted.
indexnumberElement index, not byte offset.
valuenumber | bigintOperand matching the view numeric type.

Return Value

number | bigint

Previous value.

Working Example

Example Snippetjavascript
const a=new Int32Array(new SharedArrayBuffer(4)); a[0]=12; console.log(Atomics.exchange(a,0,5),a[0]);
JavaScript Reference