Understanding Buffers

A Buffer represents a fixed-size sequence of bytes. Buffers are important when working with files, network protocols, cryptography, images, compressed data, and other binary content.

Buffers are built on top of JavaScript typed-array capabilities but provide Node-specific APIs for binary data.

Creating and Reading a Buffer
const buffer = Buffer.from('Hello Node.js', 'utf8');

console.log(buffer);
console.log(buffer.toString('utf8'));
💡 Buffer.from() converts text into bytes, while toString() converts those bytes back into text.
Key Takeaways
  • Buffers represent binary byte sequences.
  • Buffers are commonly used by Node.js I/O APIs.
  • Always know the encoding when converting between bytes and strings.
Knowledge Check

What does a Node.js Buffer represent?