method exchanges a given replacement value at a given position in the array, if a given
expected value equals the old value. It returns the old value at that position whether
it was equal to the expected value or not. This atomic operation guarantees that no
other write happens until the modified value is written back.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Atomics.compareExchange(typedArray, index, expectedValue, replacementValue)
The old value at the given position
const sab = new SharedArrayBuffer(1024); const ta = new Uint8Array(sab); ta = 7; Atomics.compareExchange(ta, 0, 7, 12); // returns 7, the old value Atomics.load(ta, 0); // 12
The definition of 'Atomics.compareExchange' in that specification.
BCD tables only load in the browser