JavaScript DataView setBigInt64() Method
The JavaScript DataView setBigInt64() method accepts a big integer and stores it as a 64-bit signed integer in the 8-byte segment starting at the specified byte offset within this data view. Additionally, you can store multiple byte values at any offset within the bounds.
This method throws a 'RangeError' exception if the value of the byteOffset parameter falls outside this bound. If the given value does not fit for the bigInt signed integer, it will throw a 'TypeError' exception.
Syntax
Following is the syntax of the JavaScript DataView setBigInt64() method −
setBigInt64(byteOffset, value, littleEndian)
Parameters
This method accepts three parameters 'byteOffset', 'value', and 'littleEndian', which are described below −
- byteOffset − The position in the DataView where the byte will be stored.
- value − A signed 64-bit integer that needs to be stored.
- littleEndian − It indicates whether the data is stored in little-endian or big-endian format.
Return value
This method returns 'undefined'.
Example 1
The following program demonstrates the usage of the JavaScript DataView setBigInt64() 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);
//using the setBigInt64() method
data_view.setBigInt64(byteOffset, value);
document.write("<br>The stored value is: ", data_view.getBigInt64(byteOffset));
</script>
</body>
</html>
Output
The above program stores the specified bigInt signed value within the current DataView and displays it as −
The byte offset: 0 Value: 9223372036854775807 The stored value is: 9223372036854775807
Example 2
If you try to print the result of this method, it will return an 'undefined' as the output.
The following is another example of the JavaScript DataView setBigInt64() method. We use this method to store a BigInt value as a 64-bit signed integer, starting at the specified byteOffset value 1 within this data view.
<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);
//using the setBigInt64() method
document.write("<br>The data_view.setBigInt64() method returns: ", data_view.setBigInt64(byteOffset, value));
</script>
</body>
</html>
Output
After executing the above program, it will return an 'undefined' result.
The byte offset: 1 Value: 9223372036854775807 The data_view.setBigInt64() method returns: undefined
Example 3
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);
try {
//using the setBigInt64() method
data_view.setBigInt64(byteOffset, value);
} catch (error) {
document.write("<br>", error);
}
</script>
</body>
</html>
Output
Once the above program is executed, it will throw a 'RangeError' exception as −
The byte offset: -1 Value: 9223372036854775807 RangeError: Offset is outside the bounds of the DataView
Example 4
If the given value does not fit for the bigInt signed integer, this method will throw a 'TypeError' 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 = 2345678765432;
document.write("The byte offset: ", byteOffset);
document.write("<br>Value: ", value);
try {
//using the setBigInt64() method
data_view.setBigInt64(byteOffset, value);
} catch (error) {
document.write("<br>", error);
}
</script>
</body>
</html>
Output
The above program thorws a 'TypeError' exception as −
The byte offset: 1 Value: 2345678765432 TypeError: Cannot convert 2345678765432 to a BigInt