Computer >> Computer tutorials >  >> Programming >> Javascript

Atomics.store() function in JavaScript


The Atomic object of JavaScript is an object and which provides atomic operations such as add, sub, and, or, xor, load, store etc. as static methods, these methods are used with SharedArrayBuffer objects.

The store() function of the atomic object accepts a number(value) and a position in the array and, stores the given value in the specified position and returns the same.

Syntax

Its syntax is as follows

Atomics.store()

Example

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var arrayBuffer = new SharedArrayBuffer(16);
      var data = new Uint8Array(arrayBuffer);
      data[0] = 20;
      Atomics.store(data, 0, 30);
      document.write(Atomics.load(data, 0));
   </script>
</body>
</html>

Output

30

Example

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var arrayBuffer = new SharedArrayBuffer(16);
      var data = new Uint8Array(arrayBuffer);
      document.write(Atomics.store(data, 0, 10));
      document.write("<br>");
      document.write(Atomics.store(data, 1, 20));
      document.write("<br>");
      document.write(Atomics.store(data, 2, 30));
      document.write("<br>");
      document.write(Atomics.store(data, 3, 40));
   </script>
</body>
</html>

Output

10
20
30
40