Atomics.sub() 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 sub() function of the atomic object accepts a number and the position, subtracts the given number from the number in the given position and it returns the value of the number in the old position.

Syntax

Its syntax is as follows.

Atomics.sub()

Example

 Live Demo

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

Output

20

Example

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var arrayBuffer = new SharedArrayBuffer(16);
      var data = new Uint8Array(arrayBuffer);
      data[0] = 30;
      document.write("Previous: "+Atomics.sub(data, 0, 10));
      document.write("<br>");
      document.write("Result: "+Atomics.load(data, 0));
      document.write("<br>");
      document.write("Previous: "+Atomics.sub(data, 0, 5));
      document.write("<br>");
      document.write("Result: "+Atomics.load(data, 0));
   </script>
</body>
</html>

Output

Previous: 30
Result: 20
Previous: 20
Result: 15

Updated on: 25-Jun-2020

83 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements