JavaScript | arrayBuffer.byteLength

The arrayBuffer.byteLength is a property in JavaScript which gives the length of an arrayBuffer in bytes.
Syntax:

ArrayBuffer.byteLength

Parameters: It doesn’t 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:

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
    // Creation of arrayBuffer of size 5 bytes
    var A = new ArrayBuffer(5);
  
    // Using property byteLength
    var bytes1 = A.byteLength;
  
    // Printing the lengths of the ArrayBuffer
    document.write(bytes1);
  
</script>

chevron_right


Output:

5

Errors and Exception:

If length of arrayBuffer is given in fraction, it returns length in whole number and when length is given in the form of string then it returns length of zero (0).
Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
    // Creation of arrayBuffer of sizes 5.6
    var A = new ArrayBuffer(5.6);
  
   // Using property byteLength
   var bytes1 = A.byteLength;
  
   // Printing the length of the ArrayBuffer
   document.write(bytes1);
  
</script>

chevron_right


Output:

5

Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
    // Creation of arrayBuffers of sizes "a" bytes
    var A = new ArrayBuffer("a");
  
    // Using property byteLength
    var bytes1 = A.byteLength;
  
    // Printing the length of the ArrayBuffer
    document.write(bytes1);
  
</script>

chevron_right


Output:

0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.