Skip to main content
Skip to main content

from

BufferConstructor.from

from(arrayBuffer, byteOffset?, length?): 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 'buffer';

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

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 internalBuffer pool like Buffer.allocUnsafe() does.

Parameters

arrayBufferWithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>Required
byteOffsetnumber
lengthnumber

Returns

Buffer

BufferBufferRequired

Since

v5.10.0

from(data): Buffer

Creates a new Buffer using the passed {data}

Parameters

dataUint8Array | readonly number[]Required
data to create a new Buffer

Returns

Buffer

BufferBufferRequired

from(data): Buffer

Parameters

dataWithImplicitCoercion<string | Uint8Array | readonly number[]>Required

Returns

Buffer

BufferBufferRequired

from(str, encoding?): 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

strWithImplicitCoercion<string> | { [toPrimitive]: Method [toPrimitive] }Required

Returns

Buffer

BufferBufferRequired
Was this section helpful?