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.