JavaScript DataView setBigUint64() Method
The JavaScript DataView setBigUint64() method accepts a big integer and stores it as a 64-bit unsigned integer in the 8-byte segment starting from a specified byte offset within this DataView.
There are no strict alignment requirements; you can store multiple bytes at any offset within the bounds of the DataView.
This method throws a 'RangeError' exception if the value of the byteOffset parameter falls outside this bound, and if the given value does not fit for the bigInt unsigned integer, it will throw a 'TypeError' exception.
Syntax
Following is the syntax of the JavaScript DataView setBigUint64() method −
setBigUint64(byteOffset, value, littleEndian)
Parameters
This method accepts three parameters named 'byteOffset', 'value', and 'littleEndian', which are described below −
- byteOffset − The position in the DataView where the byte will be stored.
- value − An unsigned 64-bit big 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', as it only stores the byte value.
Example 1
The following is the basic example of the JavaScript DataView setBigUint64() method.
<html>
<body>
<script>
const buffer = new ArrayBuffer(16);
const data_view = new DataView(buffer);
const byteOffset = 0;
//find the highest possible BigInt value that fits in an unsigned 64-bit integer
const value = 2n ** 64n - 1n;
document.write("The byte offset: ", byteOffset);
document.write("<br>Value: ", value);
//using the setBigUnit64() method
data_view.setBigUint64(byteOffset, value);
document.write("<br>The stored value: ", data_view.getBigUint64(byteOffset));
</script>
</body>
</html>
Output
The above program stores the specified bigInt unsigned value within the current DataView and displays it as −
The byte offset: 0 Value: 18446744073709551615 The stored value: 18446744073709551615
Example 2
If you try to print the result of this method, it will return an 'undefined' as the output.
<html>
<body>
<script>
const buffer = new ArrayBuffer(16);
const data_view = new DataView(buffer);
const byteOffset = 1;
//find the highest possible BigInt value that fits in an unsigned 64-bit integer
const value = 2n ** 64n - 1n;
document.write("The byte offset: ", byteOffset);
document.write("<br>Value: ", value);
//using the setBigUnit64() method
document.write("<br>The data_view.setBigUnit64() method returns: ", data_view.setBigUint64(byteOffset, value));
</script>
</body>
</html>
Output
After executing the above program, it will return an 'undefined' result.
The byte offset: 1 Value: 18446744073709551615 The data_view.setBigUnit64() 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 = 0;
//find the highest possible BigInt value that fits in an unsigned 64-bit integer
const value = 23456543212;
document.write("The byte offset: ", byteOffset);
document.write("<br>Value: ", value);
try {
//using the setBigUnit64() method
data_view.setBigUint64(byteOffset, value);
} catch (error) {
document.write("<br>", error);
}
</script>
</body>
</html>
Output
Once the above program is executed, it will throw a 'RangeError' exception.
The byte offset: 0 Value: 23456543212 TypeError: Cannot convert 23456543212 to a BigInt
Example 4
If the given value does not fit for the bigInt unsigned integer, this method will throw a 'TypeError' exception.
<html>
<head>
<title>JavaScript DataView setBigUint64() Method</title>
</head>
<body>
<script>
const buffer = new ArrayBuffer(16);
const data_view = new DataView(buffer);
const byteOffset = 0;
//find the highest possible BigInt value that fits in an unsigned 64-bit integer
const value = 23456543212;
document.write("The byte offset: ", byteOffset);
document.write("<br>Value: ", value);
try {
//using the setBigUnit64() method
data_view.setBigUint64(byteOffset, value);
} catch (error) {
document.write("<br>", error);
}
</script>
</body>
</html>
Output
The above program throws a 'TypeError' exception as −
The byte offset: 0 Value: 23456543212 TypeError: Cannot convert 23456543212 to a BigInt