Open In App

JavaScript arrayBuffer.byteLength Property

The Javascript arrayBuffer.byteLength is a property in JavaScript that gives the length of an arrayBuffer in bytes. 

Syntax:



ArrayBuffer.byteLength

Parameters: It does not accept any parameter because arrayBuffer.byteLength is a property, not a function.

Return Value: It returns the length of an arrayBuffer in bytes.



JavaScript code to show the working of the arrayBuffer.byteLength property:

Example: This is a basic example showing the usage of arrayBuffer.byteLength property.




// Creation of arrayBuffer of size 5 bytes
let A = new ArrayBuffer(5);
 
// Using property byteLength
let bytes1 = A.byteLength;
 
// Printing the lengths of the ArrayBuffer
console.log(bytes1);

Output
5

Errors and Exceptions:

If the length of arrayBuffer is given in fractions, it returns length in the whole number and when the length is given in the form of string then it returns a length of zero (0). 

Example 1: In this example, we will pass the length of arrayBuffer in fractions.




// Creation of arrayBuffer of sizes 5.6
let A = new ArrayBuffer(5.6);
 
// Using property byteLength
let bytes1 = A.byteLength;
 
// Printing the length of the ArrayBuffer
console.log(bytes1);

Output
5

Example 2: In this example, we will pass the length of arrayBuffer in string format.




// Creation of arrayBuffers of sizes "a" bytes
let A = new ArrayBuffer("a");
 
// Using property byteLength
let bytes1 = A.byteLength;
 
// Printing the length of the ArrayBuffer
console.log(bytes1);

Output
0


Article Tags :