• JavaScript Video Tutorials

JavaScript DataView getBigInt64() Method



The JavaScript DataView getBigInt64() method is used to retrieve an 8-byte integer value starting at the specified byte offset of this DataView. It decodes these bytes as a 64-bit signed integer. Additionally, you can retrieve multiple bytes from any offset within the bounds of the DataView.

If the value of the byteOffset parameter falls outside the bounds of this DataView, and trying to retrieve data using the getBigInt64() method will throw a 'RangeError' exception.

Syntax

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

getBigInt64(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 a BigInt in range from -263 to 263-1, included.

Example 1

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

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const byteOffset = 0;
   //find the highest possible BigInt value 
   const value = 2n ** (64n - 1n) - 1n;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>Value: ", value);
   //storing the value
   data_view.setBigInt64(byteOffset, value);
   //using the getBigInt64() method
   document.write("<br>The store value: ", data_view.getBigInt64(byteOffset));
</script>
</body>
</html>

Output

The above program returns the stored value.

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

Example 2

If the value of the byteOffset parameter falls 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 byteOffset = 1;
   //find the highest possible BigInt value 
   const value = 2n ** (64n - 1n) - 1n;
   document.write("The byte offset: ", byteOffset);
   document.write("<br>Value: ", value);
   //storing the value
   data_view.setBigInt64(byteOffset, value);
   try {
     //using the getBigInt64() method
     document.write("<br>The store value: ", data_view.getBigInt64(-1));
   } catch (error) {
     document.write("<br>", error);
   }
</script>
</body>
</html>

Output

After executing the above program, it will throw an exception as −

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