Buffer

Buffer.copy()

Copies bytes into a destination buffer. Unlike subarray, the destination does not share the copied region when separately allocated. Tested on Node.js 22.16.0. Example output (one line per entry): ["2 oo", "book"].

Formal Syntax & Structure

nodejs syntaxnodejs
source.copy(target[, targetStart[, sourceStart[, sourceEnd]]])

Parameters & Attributes

NameTypeDescription
targetBuffer | Uint8ArrayDestination.
targetStartnumberDestination offset.
sourceStartnumberSource offset.
sourceEndnumberExclusive source end.

Return Value

number

Bytes copied.

Working Example

Example Snippetnodejs
const a = Buffer.from('book'), b = Buffer.alloc(2);console.log(a.copy(b, 0, 1, 3), b.toString());b[0] = 88;console.log(a.toString());
Node.js Reference