• JavaScript Video Tutorials

JavaScript DataView getInt32() Method



The JavaScript DataView getInt32() method is used to retrieve a 4-byte data value from a specified byte offset and decode it as a 32-bit signed integer. It is possible to fetch multiple byte values from any offset within the bounds of the data view.

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

Syntax

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

getInt32(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 is stored in little- or big-endian format.

Return value

This method returns an integer from -2147483648 to 2147483647, inclusive.

Example 1

The following example demonstrates the usage of the JavaScript DataView getInt32() method.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(32);
   const data_view = new DataView(buffer);
   const value = 2147483647;
   const byteOffset = 0;
   document.write("Value: ", value);
   document.write("<br>The byte offset: ", byteOffset);
   //storing value
   data_view.setInt32(byteOffset, value);
   document.write("<br>The store value: ", data_view.getInt32(byteOffset));
   </script>
</body>
</html>

Output

The above program produces the stored value as −

Value: 2147483647
The byte offset: 0
The store value: 2147483647

Example 2

If the byteOffset parameter value is not passed to this method, it will automatically return the stored value at byteOffset 0 of this data view.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(32);
   const data_view = new DataView(buffer);
   const value = 2443;
   const byteOffset = 0;
   document.write("Value: ", value);
   document.write("<br>The byte offset: ", byteOffset);
   //storing value
   data_view.setInt32(byteOffset, value);
   document.write("<br>The data_view.getInt32() method returns: ", data_view.getInt32());
</script>
</body>
</html>

Output

After executing the above program, it will return the stored value as '2443'.

Value: 2443
The byte offset: 0
The data_view.getInt32() method returns: 2443

Example 3

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

<html>
<body>
<script>
   const buffer = new ArrayBuffer(32);
   const data_view = new DataView(buffer);
   const value = 2443;
   const byteOffset = 0;
   document.write("Value: ", value);
   document.write("<br>The byte offset: ", byteOffset);
   //storing value
   data_view.setInt32(byteOffset, value);
   try {
      document.write(data_view.getInt32(-1));
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

Output

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

Value: 2443
The byte offset: 0
RangeError: Offset is outside the bounds of the DataView
Advertisements