Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

ArrayBuffer.prototype.resize()

Baseline2024
Newly available

Theresize() method ofArrayBuffer instances resizes theArrayBuffer to the specified size, in bytes.

Try it

const buffer = new ArrayBuffer(8, { maxByteLength: 16 });console.log(buffer.byteLength);// Expected output: 8buffer.resize(12);console.log(buffer.byteLength);// Expected output: 12

Syntax

js
resize(newLength)

Parameters

newLength

The new length, in bytes, to resize theArrayBuffer to.

Return value

None (undefined).

Exceptions

TypeError

Thrown if theArrayBuffer is detached or is not resizable.

RangeError

Thrown ifnewLength is larger than themaxByteLength of theArrayBuffer.

Description

Theresize() method resizes anArrayBuffer to the size specified by thenewLength parameter, provided that theArrayBuffer isresizable and the new size is less than or equal to themaxByteLength of theArrayBuffer. New bytes are initialized to 0.

Note that you can useresize() to shrink as well as grow anArrayBuffer — it is permissible fornewLength to be smaller than theArrayBuffer's currentbyteLength.

Examples

Using resize()

In this example, we create a 8-byte buffer that is resizable to a max length of 16 bytes, then check itsresizable property, resizing it ifresizable returnstrue:

js
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });if (buffer.resizable) {  console.log("Buffer is resizable!");  buffer.resize(12);}

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-arraybuffer.prototype.resize

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp