• JavaScript Video Tutorials

JavaScript DataView getInt16() Method



The JavaScript DataView getInt16() method is used to retrieve 2-byte data starting at the specified byte offset in the DataView, and the retrieved data will be decoded as a 16-bit signed integer. This method can also fetch multiple-byte values from any offset within the bounds.

If we do not pass the byteOffset parameter to this method, it will return 0, and if the byteOffset parameter falls outside the bounds of the data view, it will throw a 'RangeError' exception.

Syntax

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

getInt16(byteOffset, littleEndian)

Parameters

This method accepts two parameters named 'byteOffset' and 'littleEndian', which are described below −

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

Return value

This method returns an integer from the range of -32768 to 32767, inclusive.

Example 1

The following is the basic example of the JavaScript DataView getInt16() method.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const value = 32767;
   const byteOffset = 1;
   document.write("Value: ", value);
   document.write("<br>The byte offset: ", byteOffset);
   //storing the data
   data_view.setInt16(byteOffset, value);
   //using the getInt16() method
   document.write("<br>The store value: ",  data_view.getInt16(byteOffset));
</script>
</body>
</html>

Output

The program mentioned above will return the value that has been stored.

Value: 32767
The byte offset: 1
The store value: 32767

Example 2

If we do not pass the byteOffset parameter in this method, it will return 0.

The following is another example of the JavaScript DataView getInt16() method to retrieve 2-byte data value 3405, which is stored by the setInt16() method at the specified byte offset 0.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const value = 3405;
   const byteOffset = 0;
   document.write("Value: ", value);
   document.write("<br>The byte offset: ", byteOffset);
   //using the getInt16() method
   data_view.getInt16(byteOffset, value);
   document.write("<br>The data_view.getInt16() method: ", data_view.getInt32());
</script>
</body>
</html>

Output

After executing the above program, it will return 0.

Value: 3405
The byte offset: 0
The data_view.getInt16() method: 0

Example 3

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

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const value = 255;
   const byteOffset = 1;
   document.write("Value: ", value);
   document.write("<br>The byte offset: ", byteOffset);
   try {
      //using the getInt16(-1) method 
      data_view.getInt16(-1);
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Output

Once the above program is executed, it will throw a 'RangeError' exception.

Value: 255
The byte offset: 1
RangeError: Offset is outside the bounds of the DataView
Advertisements