Open In App

JavaScript arrayBuffer.byteLength Property

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

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.

javascript




// 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.

javascript




// 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.

javascript




// 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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads