• JavaScript Video Tutorials

JavaScript DataView getUint16() Method



The JavaScript DataView getUint16() method is used to read 16-bit (or 2-byte) data starting at the specified byte offset of this data view and interprets them as a 16-bit unsigned integer (uint). If this parameter is not passed or not specified to this method, it will always return 16.

If the value of the byteOffset parameter is outside the bounds of this data view, it will throw a 'RangeError' exception.

Syntax

Following is the syntax of the JavaScript DataView getUint16() method −

getUint16(byteOffset, littleEndian)

Parameters

  • byteOffset − The position in the DataView from which to read the data.
  • littleEndian − It indicates whether the data is stored in little-endian or big-endian format.

Return value

This method returns an integer from 0 to 65535, inclusive.

Example 1

In the following example, we are using the JavaScript DataView getUint16() method to read the value 459 stored by the setUnit16() method at the specified byte offset 0.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const value = 459;
   const byteOffset = 0;
   document.write("The data value: ", value);
   document.write("<br>The byteOffset: ", byteOffset);
   //storing the value
   data_view.setUint16(byteOffset, value);
   //using the getUnit16() method
   document.write("<br>The stored value: ", data_view.getUint16(byteOffset));
</script>
</body>
</html>

Output

The above program returns the read value as 225.

The data value: 459
The byteOffset: 0
The stored value: 459

Example 2

If the value of the byteOffset parameter is outside the bounds of the data view, it will throw a 'RangeError' exception.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const value = 300;
   const byteOffset = 1;
   document.write("The data value: ", value);
   document.write("<br>The byteOffset: ", byteOffset);
   try {
      //storing value
      data_view.setUint16(byteOffset, value);
	  //using the getUnit16() method
      document.write("<br>The stored value: ", data_view.getUint16(-1));
   }catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Output

After executing the above program, it will throw a 'RangeError' exception as −

The data value: 300
The byteOffset: 1
RangeError: Offset is outside the bounds of the DataView

Example 3

If the byteOffset parameter is not passed to this method, it will return a result as 16.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const value = 4234;
   const byteOffset = 1;
   document.write("The data value: ", value);
   document.write("<br>The byteOffset: ", byteOffset);
   data_view.setUint16(byteOffset, value);
   //using the getUnit16() method
   document.write("<br>The data_view.getUnit16() method returns: ", data_view.getUint16());//not passing byteOffset
</script>
</body>
</html>

Output

Once the above program is executed, it will return 16.

The data value: 4234
The byteOffset: 1
The data_view.getUnit16() method returns: 16
Advertisements