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

NameTypeDescription
sizenumberNumber of bytes.
fillstring | Buffer | Uint8Array | integerValue or repeated byte pattern.
encodingstringEncoding 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'));
Node.js Reference