DevAcademy
IntermediateNode.js

Buffers

Work with raw binary data directly — the type streams and the file system deal in under the hood.

Reading Time

10 min

Lesson

Lesson 12 of 34

What a Buffer Is

A Buffer represents a fixed-length chunk of raw binary data, similar to an array of bytes. It exists because JavaScript strings are built for text, not binary data — reading an image file or a network packet needs something else.

Creating Buffers

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Converting Between Buffers and Strings

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Where Buffers Show Up

ContextExample
File systemfs.readFile() without an encoding returns a Buffer
StreamsReadable streams emit Buffer chunks by default
NetworkingRaw TCP/HTTP data arrives as Buffers
CryptographyHashes and encrypted data are commonly Buffers

Buffer Length is Bytes, Not Characters

buf.length reports the number of bytes, not characters — a string with multi-byte UTF-8 characters (like emoji or accented letters) will have a buffer length larger than the visible character count.

Best Practice

You rarely need to create Buffers manually in everyday application code — most high-level APIs (fs.readFile with an encoding, HTTP body parsers) hand you a string or parsed object already. Reach for Buffer directly when working with genuinely binary data.

Interview Questions

Quick Quiz

1. What does a Buffer represent?

2. Does Buffer.length measure bytes or characters?

3. What type of data do readable streams emit by default?