Standard built-ins
Atomics.compareExchange()
Replaces the element if it equals expectedValue and returns the old element value in either case.
Formal Syntax & Structure
javascript syntaxjavascript
Atomics.compareExchange(typedArray, index, expectedValue, replacementValue)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| typedArray | IntegerTypedArray | Eligible integer view. |
| index | number | Element index. |
| expectedValue | number | bigint | Expected old value. |
| replacementValue | number | bigint | Replacement if comparison succeeds. |
Return Value
number | bigint
Replaces the element if it equals expectedValue and returns the old element value in either case.
Working Example
Example Snippetjavascript
(async () => {const a=new Int32Array(new SharedArrayBuffer(4));Atomics.store(a,0,2);console.log(Atomics.compareExchange(a,0,9,7),Atomics.load(a,0));console.log(Atomics.compareExchange(a,0,2,7),Atomics.load(a,0));})()