Atomics - and() Method

and method computes the bitwise AND with a provided value at a given position in the array. It returns the old value at that position. This atomic operation ensures that no other write can happen until the modified value is written back.


Atomics.and(typedArray, index, value)


  • typedArray is the integer typed array.

  • index is position in typedarray.

  • value with which bitwise AND to be computed.


Returns old value at given position.


  • TypeError in case passed array is not a integer typed array.

  • RangeError if index passed is out of bound in typed array.


Following is the code for implementing JavaScript Atomics −

<!DOCTYPE html>
<html lang="en">
      <meta charset="UTF-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Atomics Example</title>
         .result {
            font-size: 20px;
            border: 1px solid black;
   <body onLoad="operate();">
      <h1>JavaScript Atomics Properties</h1>
      <div class="result"></div>
      <p>Atomics.and(arr, 0, 2)</p>
      <p>Atomics.load(arr, 0)</p>
         function operate(){
            let container = document.querySelector(".result");
            // create a SharedArrayBuffer
            var buffer = new SharedArrayBuffer(25);
            var arr = new Uint8Array(buffer);
            // Initialise element at zeroth position of array with 6
            arr[0] = 6;
            //6 & 2 = 110 & 010 = 010 = 2
            container.innerHTML = Atomics.and(arr, 0, 2) + '<br/>'+ Atomics.load(arr, 0);


Verify the result.

Kickstart Your Career

Get certified by completing the course

Get Started