What is Buffer in Node.js?
Buffer is a special data type in Node.js for working with binary (raw) data, meaning sequences of bytes, rather than regular JavaScript strings or objects.
1. What Buffer is, in plain words
When Node.js works with files, networking, images, encryption, and other low-level I/O operations, it often gets not text, but bytes (zeros and ones).
The problem: JavaScript, originally (in the browser), can't work with binary data directly, it only has strings and objects.
The fix:
Node.js introduced the Buffer object, a structure that:
- holds a sequence of raw bytes;
- lets you read and write those bytes in different formats (UTF-8, Base64, HEX, and so on);
- is used in every I/O operation (files, HTTP, TCP, streams).
2. An example of using Buffer
const buf = Buffer.from('Hello', 'utf8');
console.log(buf); // <Buffer 48 65 6c 6c 6f>
console.log(buf.toString()); // "Hello"Here:
'Hello'is a string,Buffer.from()created a buffer from it,- the bytes themselves are stored inside (
0x48,0x65, ...), toString()turns the bytes back into text.
3. Where Buffer is used
Node.js uses Buffer everywhere binary data needs handling:
| Example | Description |
|---|---|
fs.readFile() | reads a file into a Buffer |
http | a request or response body arrives as a byte stream |
net.Socket | passing binary data over TCP |
zlib, crypto | compression, encryption |
stream | every stream reads/writes Buffer objects specifically |
4. Main ways to create a Buffer
// 1. Create a buffer from a string
const buf1 = Buffer.from('Node.js');
// 2. Create an empty buffer of a given size
const buf2 = Buffer.alloc(10); // 10 bytes, filled with zeros
// 3. Create an "unsafe" buffer (not zeroed)
const buf3 = Buffer.allocUnsafe(10); // faster, but may hold "garbage"
// 4. Create one from an array of numbers
const buf4 = Buffer.from([0x41, 0x42, 0x43]);
console.log(buf4.toString()); // "ABC"5. Reading and writing data in a Buffer
const buf = Buffer.alloc(4);
buf.writeUInt8(65, 0); // Write the number 65 (A) into byte #0
buf.writeUInt8(66, 1); // B
buf.writeUInt8(67, 2); // C
console.log(buf.toString('utf8')); // "ABC"It can be read back as numbers, strings, floats, hex, and so on.
6. Interaction with streams
When reading a file, a stream returns data as buffers:
const fs = require('fs');
const stream = fs.createReadStream('file.txt');
stream.on('data', chunk => {
console.log('Data type:', Buffer.isBuffer(chunk)); // true
console.log('A piece of the file:', chunk.toString());
});Each chunk is a Buffer representing a piece of the file.
7. Working with encodings
Buffer supports several encodings:
'utf8''ascii''base64''hex''latin1''utf16le'
const buf = Buffer.from('Hello', 'utf8');
console.log(buf.toString('hex')); // the hex representation of the text8. Why Buffer matters
| Advantage | Explanation |
|---|---|
| Efficiency | Works directly with bytes, no intermediate conversions. |
| Low-level integration | Needed to interact with the OS, the network, the filesystem. |
| Compatibility | A universal exchange format across streams, HTTP, TCP, and so on. |
9. How Buffer differs from Array or String
| Property | Buffer | Array | String |
|---|---|---|---|
| Stores | bytes | arbitrary values | characters |
| Mutability | mutable | mutable | immutable |
| Size | fixed | dynamic | dynamic |
| Purpose | binary data | logic and computation | text |
Summary
Bufferin Node.js is an object for working with raw binary data. It's used in every I/O operation, files, network, streams, letting you read, write, and convert bytes directly, providing high performance and low-level control over data.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.