Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

buffer
nodejs
communitycreator

What is the Buffer.from() method in Node.js?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

In Node.js, the Buffer.from() method creates a new buffer filled with the specified string, array, buffer, or arrayBuffer.

This method copies buffer data onto a new Buffer instance.

Syntax

Buffer.from(obj, encoding);

Parameters

  • obj: This is required. And it could be a string, array, or buffer.

  • endcoding: This is optional. It is the encoding type. When not specified, Node.js uses utf8as default.

Return value

The Buffer.from() returns a buffer object.

Example

In this example, we will create a buffer using the Buffer.from(), access the buffer, and check the return value.

// Create a buffer
let buffer1 = Buffer.from("hello", "utf8")

// log the buffer to the console
console.log(buffer1);

// Check the instance of buffer
console.log(buffer1 instanceof Buffer)

In the example above, we created a buffer using a string. We logged it to the console. Then we checked the instance of the buffer to see whether it is actually an instance of the Buffer object, and whether it was true.

RELATED TAGS

buffer
nodejs
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring