Skip to main content

from

method BufferConstructor.from
Jump to headingBufferConstructor.from(
arrayBuffer: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>,
byteOffset?: number,
length?: number,
): Buffer

Allocates a new Buffer using an array of bytes in the range 0255. Array entries outside that range will be truncated to fit into it.

import { Buffer } from 'node:buffer';

// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.
const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);

If array is an Array-like object (that is, one with a length property of type number), it is treated as if it is an array, unless it is a Buffer or a Uint8Array. This means all other TypedArray variants get treated as an Array. To create a Buffer from the bytes backing a TypedArray, use Buffer.copyBytesFrom().

A TypeError will be thrown if array is not an Array or another type appropriate for Buffer.from() variants.

Buffer.from(array) and Buffer.from(string) may also use the internal Buffer pool like Buffer.allocUnsafe() does.

Parameters Jump to heading

Jump to headingarrayBuffer: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>
optional
Jump to headingbyteOffset: number
optional
Jump to headinglength: number

Return Type Jump to heading

Jump to headingBufferConstructor.from(data: Uint8Array | readonly number[]): Buffer

Creates a new Buffer using the passed {data}

Parameters Jump to heading

Jump to headingdata: Uint8Array | readonly number[]

data to create a new Buffer

Return Type Jump to heading

Jump to headingBufferConstructor.from(data: WithImplicitCoercion<
Uint8Array
| readonly number[]
| string
>
): Buffer

Parameters Jump to heading

Jump to headingdata: WithImplicitCoercion<
Uint8Array
| readonly number[]
| string
>

Return Type Jump to heading

Jump to headingBufferConstructor.from(
str: WithImplicitCoercion<string> | { [[Symbol.toPrimitive]](hint: "string"): string; },
encoding?: BufferEncoding,
): Buffer

Creates a new Buffer containing the given JavaScript string {str}. If provided, the {encoding} parameter identifies the character encoding. If not provided, {encoding} defaults to 'utf8'.

Parameters Jump to heading

Jump to headingstr: WithImplicitCoercion<string> | { [[Symbol.toPrimitive]](hint: "string"): string; }

Return Type Jump to heading

Back to top