Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript arrayBuffer.byteLength Property

Improve Article
Save Article
Like Article
  • Last Updated : 09 Dec, 2022
Improve Article
Save Article
Like Article

The Javascript arrayBuffer.byteLength is a property in JavaScript which 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




<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
    console.log(bytes1);
</script>

Output:

5

Errors and Exception: 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




<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
    console.log(bytes1);
</script>

Output:

5

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

javascript




<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
    console.log(bytes1);
</script>

Output:

0

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!