DataView.setUint16() function in JavaScript

JavascriptWeb DevelopmentFront End Scripts

The setUint16() function of the DataView stores an unsigned 16-bit integer at the specified position.

Syntax

Its syntax is as follows

dataView.setUint16();

Example

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var arrayBuffer = new ArrayBuffer(20);
      var dataView = new DataView(arrayBuffer);
      dataView.setUint16(1, 45544);
      document.write(dataView.getUint16(1));
   </script>
</body>
</html>

Output

45544

Example

To this function you cannot pass float value, if you try to do so it is considered as integer value.

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var arrayBuffer = new ArrayBuffer(20);
      var dataView = new DataView(arrayBuffer);
      dataView.setUint16(1, 455.44);
      document.write(dataView.getUint16(1));
   </script>
</body>
</html>

Output

455

Example

If nothing is stored in the dataview and if you still, try to get data, this function will return 0.

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var arrayBuffer = new ArrayBuffer(20);
      var dataView = new DataView(arrayBuffer);
      dataView.setUint16(1);
      document.write(dataView.getUint16(1));
   </script>
</body>
</html>

Output

0
raja
Published on 24-Oct-2018 12:02:23
Advertisements