Open In App

JavaScript BigUint64Array() Constructor

Last Updated : 25 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The BigUint64Array() Constructor creates a new typed array of the 64-bit unsigned integers (BigInts). Typed arrays are a way to handle and manipulate binary data in a specific format. It allows to create arrays for storing large unsigned 64-bit integers. It is part of the JavaScript TypedArray object family which provides efficient memory management and manipulation of the binary data.

The JavaScript’s native number type is limited to a 53-bit precision, which means it cannot accurately represent integers beyond that range. However, some applications such as cryptography or low-level data manipulation require operations on larger numbers. The BigUint64Array comes in handy for handling these scenarios.

Syntax:

new BigUint64Array(length);
new BigUint64Array(typedArray);
new BigUint64Array(object);
new BigUint64Array(buffer [ byteOffset [length]]);

The BigUint64Array() constructor creates a new BigUint64Array object with a specified length or based on the existing typedArray, object, or buffer. 

Parameters: This constructor passes the following parameters:

  • length: The length parameter represents the desired length (number of elements) of the BigUint64Array. It defaults to 0 if not provided.
  • typedArray: The typedArray parameter can be an existing BigUint64Array, BigInt64Array, Uint32Array, or any other TypedArray object whose data will be used to create the new BigUint64Array.
  • object: The object parameter can be an iterable object (such as an Array) containing numeric values to be stored in BigUint64Array.
  • buffer: The buffer parameter is an ArrayBuffer or SharedArrayBuffer that provides the underlying storage for BigUint64Array. It can optionally be combined with byteOffset and length to create a view over a subset of the buffer.

Return Value: A new BigUint64Array object with the specified length or with the elements copied from provided typedArray or object.

Usage:

  • Creating a BigUint64Array with a specified length
// Creates a BigUint64Array with the 5 elements initialized to 0
const array = new BigUint64Array(5);
  • Creating a BigUint64Array based on an existing typedArray
// Creates a BigUint64Array with same values as existingArray
const existingArray = new Uint32Array([10, 20, 30]);
const newArray = new BigUint64Array(existingArray);
  • Creating a BigUint64Array from an iterable object
// Creates a BigUint64Array with the values from iterable object
const iterable = [100n, 200n, 300n];
const array = new BigUint64Array(iterable);
  • Creating a BigUint64Array with a subset of a buffer
// Creates a BigUint64Array with 4 elements, starting at byte offset 8.
const buffer = new ArrayBuffer(32);
const array = new BigUint64Array(buffer, 8, 4);

Example 1: Creating an empty BigUint64Array with a specific length.

Javascript




const array = new BigUint64Array(2);
console.log(array);


Output:

BigUint64Array(2) [ 0n, 0n ]

Example 2: Creating a BigUint64Array from an iterable object (e.g., an array).

Javascript




const iterable = [150n, 210n, 400n];
const bigUint64Array = new BigUint64Array(iterable);
console.log(bigUint64Array);


Output:

BigUint64Array(3) [ 150n, 210n, 400n ]

Example 3: Accessing a subset of a BigUint64Array

Javascript




const bigArray = new BigUint64Array([200n, 300n, 200n, 400n, 700n]);
const subArray = bigArray.subarray(1, 3);
console.log(subArray);


Output:

BigUint64Array(2) [ 300n, 200n ]

Example 4: Modifying a BigUint64Array element

Javascript




const bigArray = new BigUint64Array([20n, 30n, 30n]);
bigArray[2] = 50n;
console.log(bigArray);


Output:

BigUint64Array(3) [ 20n, 30n, 50n ]

Supported Browsers:

  • Google Chrome 67
  • Mozilla Firefox 68
  • Microsoft Edge 79
  • Safari 15
  • Opera 54

In Conclusion, The BigUint64Array() Constructor provides a way to work with large unsigned 64-bit integers in an array-like structure. It allows you to create and manipulate arrays of these large integers, enabling operations on numbers that exceed the usual JavaScript numeric limits. Whether you need to handle cryptographic operations, bit manipulation, or other scenarios requiring large integer arithmetic. BigUint64Array provides a useful tool in JavaScript.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads