JavaScript | arrayBuffer.byteLength property

The arrayBuffer.byteLength is a property in JavaScript which return the length of an ArrayBuffer in byte. ArrayBuffer is an object which is used to represent fixed-length binary data.
Difference between property and function in javascript.
Property in JavaScript is nothing but a value whereas method is a function this can be understood with the help of a example given below.

filter_none

edit
close

play_arrow

link
brightness_4
code

// car is an object.
var car = {};
  
// car.name is a property of the given object.
car.name = "Audi",
  
// car.sayModel is a function of the given object.
car.sayModel = function() {
console.log("A8");
}
      
// printing property value.
console.log(car.name);
  
// printing function called value.
console.log(car.sayModel());

chevron_right


Output:

> "Audi"
> "A8"

Here as we can see that property of the object car, is going to store the string as “Audi” and it can be accessed with car.name.
sayModel is a method i.e, a function of the object and it can be accessed with car.sayModel().
It can be noticed that sayModel is just a function which uses ().



Difference between bytelength and length property-
Length property returns the length of a String object i.e, number of characters in the string object.
Example:

Input:- geeksforgeeks
Output:- 13

Bytelength property returns the length of an ArrayBuffer in byte. ArrayBuffer is an object which is used to represent fixed-length binary data.
Example:

Input:- ArrayBuffer(2)
Output:- 2

Syntax:

arraybuffer.byteLength

    Parameter:

  • Here nothing as a parameter is being taken because this is a property, not a function.
  • Return Values:

  • It returns the length of an ArrayBuffer in byte.

Let’s see JavaScript program on this property:

  • Code #1:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // ArrayBuffer is created with 
    // some size in bytes.
    var A = new ArrayBuffer(2);
      
    // Here the byteLength property 
    // is used for checking the size.
    var B = A.byteLength;
      
    // Length of the ArrayBuffer in bytes is printed.
    console.log(B);

    chevron_right

    
    

    Output:

    > 2
  • Code #2:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // ArrayBuffer is created 
    // with some size in bytes.
    var A = new ArrayBuffer(0);
      
    // Here the byteLength property 
    // is used for checking the size.
    var B = A.byteLength;
      
    // Length of the ArrayBuffer
    // in bytes is printed.
    console.log(B);

    chevron_right

    
    

    Output:

    > 0
  • Code #3:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // ArrayBuffer is created
    // with some size in bytes.
    var A = new ArrayBuffer("geeksforgeeks");
      
    // Here the byteLength property is 
    // used for checking the size.
    var B = A.byteLength;
      
    // Length of the ArrayBuffer
    // in bytes is printed.
    console.log(B);

    chevron_right

    
    

    Output:

    > 0
  • Code #4:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // ArrayBuffer is created 
    // with some size in bytes.
    var A = new ArrayBuffer(4.6);
      
    // Here the byteLength property 
    // is used for checking the size.
    var B = A.byteLength;
      
    // Length of the ArrayBuffer 
    // in bytes is printed.
    console.log(B);

    chevron_right

    
    

    Output:


    > 4
    1. Errors and Exceptions associated with this function:

    2. Negative number can not be taken as the sizes in byte only positive integer value can be taken including zero.
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // ArrayBuffer is created 
      // with some size in bytes.
      var A = new ArrayBuffer(-2);
        
      // Here the byteLength property is
      // used for checking the size.
      var B = A.byteLength;
        
      // Length of the ArrayBuffer 
      // in bytes is printed.
      console.log(B);

      chevron_right

      
      

      Output:

      Error: Invalid array buffer length
    3. Complex number can not be taken as the sizes in byte only positive integer value can be taken including zero.
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // ArrayBuffer is created with
      // some size in bytes.
      var A = new ArrayBuffer(2 + 4i);
        
      // Here the byteLength property 
      // is used for checking the size.
      var B = A.byteLength;
        
      // Length of the ArrayBuffer
      // in bytes is printed.
      console.log(B);

      chevron_right

      
      

      Output:

      Error: Invalid or unexpected token
    4. The sizes in byte should must be less then  2^5^3 otherwise it returns Error: Invalid array buffer length.
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // ArrayBuffer is created with some size in bytes.
      var A = new ArrayBuffer(2338945720394852703948572);
        
      // Here the byteLength property is
      // used for checking the size.
      var B = A.byteLength;
        
      // Length of the ArrayBuffer in bytes is printed.
      console.log(B);

      chevron_right

      
      

      Output:

      Error: Invalid array buffer length

    Application:
    Its application is to get the length of an ArrayBuffer in byte.

  • Code #1:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // ArrayBuffer is created 
    // with some size in bytes.
    var A = new ArrayBuffer(23);
      
    // Here the byteLength property 
    // is used for checking the size.
    var B = A.byteLength;
      
    // Length of the ArrayBuffer 
    // in bytes is printed.
    console.log(B);

    chevron_right

    
    

    Output:

    > 23


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



    Improved By : Akanksha_Rai



    Article Tags :

    Be the First to upvote.


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