Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript ArrayBuffer resize() Method

JavaScript ArrayBuffer resize() Method

JavaScript resize() method in ArrayBuffer is used to increase or decrease the size of ArrayBuffer in JavaScript. This method specifies the change in length in bytes and the specified length cannot be greater than the maxByteLength property of the array Buffer.

Syntax:

resize(len)

Parameter: This method takes only a single parameter.

  • len: This is the new length of the arrayBuffer in bytes.

Return Value: An undefined value.

Example 1: This method increases the size of ArrayBuffer which can be confirmed using the byteLength() method.

Javascript




let arr = new ArrayBuffer(12, { maxByteLength: 24 });
console.log(arr.byteLength);
 
arr.resize(18);
console.log(arr.byteLength);


Output: The increased size can never be greater than the maximum byte length the of the Array Buffer

12
18

Example 2: This example shrinks the size of ArrayBuffer. We will use the byteLength() method to decrease the size.

Javascript




let arr = new ArrayBuffer(24, { maxByteLength: 24 });
console.log(arr.byteLength);
 
arr.resize(18);
console.log(arr.byteLength);


Output:

24
18

Supported Browsers:

  • Chrome
  • Edge
  • Safari

We have a complete list of ArrayBuffer methods and properties, to check Please go through the JavaScript ArrayBuffer Reference article.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments