Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript arrayBuffer byteLength Property

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The Javascript arrayBuffer.byteLength is a property in JavaScript that return the length of an ArrayBuffer in a 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 a help of an example given below. 

Syntax: 

arraybuffer.byteLength

Example: 

Input: geeksforgeeks
Output: 13

The bytelength property returns the length of an ArrayBuffer in bytes. ArrayBuffer is an object which is used to represent fixed-length binary data. 

Example: 

Input: ArrayBuffer(2)
Output: 2

Example: Here as we can see the property of the object car, is going to store the string as “Audi” and it can be accessed with car.name. The 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 that uses ().

javascript




// car is an object.
let 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());

Output: 

"Audi"
"A8"

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

Example: Let’s see the JavaScript program on this property.

javascript




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

Output: 

2

Example: In this example, we will see the usage of Array.bytelength property.

javascript




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

Output: 

0

Example: In this example, we will see the usage of Array.bytelength property.

javascript




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

Output: 

0

Example: In this example, we will see the usage of Array.bytelength property.

javascript




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

Output:  

4

Example: A negative number can not be taken as the size in byte only positive integer values can be taken including zero. 

javascript




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

Output: 

Error: Invalid array buffer length

Example: A complex number can not be taken as the sizes in bytes only positive integer values can be taken including zero. 

javascript




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

Output: 

Error: Invalid or unexpected token

Example: The sizes in byte should be less than 2^5^3        otherwise it returns Error: Invalid array buffer length. 

javascript




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

Output: 

Error: Invalid array buffer length

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

javascript




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

Output: 

23

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browser:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

My Personal Notes arrow_drop_up
Last Updated : 22 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials