Buffer
Buffer.alloc()
Creates initialized byte storage; the default contents are zero. Tested on Node.js 22.16.0. Keep allocation sizes bounded; prefer initialized storage over allocUnsafe for beginner code.
Formal Syntax & Structure
nodejs syntaxnodejs
Buffer.alloc(size[, fill[, encoding]])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| size | number | Number of bytes. |
| fill | string | Buffer | Uint8Array | integer | Value or repeated byte pattern. |
| encoding | string | Encoding for a string fill; UTF-8 by default. |
Return Value
Buffer
New initialized buffer.
Working Example
Example Snippetnodejs
import { Buffer } from 'node:buffer';console.log(Buffer.alloc(4, 7).toString('hex'));