What is a Buffer?
You'll learn what Buffers are and why Node.js uses them to handle raw binary data in memory.
The Core Idea
A Buffer is Node.js's way of working with raw binary data directly in memory. While JavaScript strings are great for text, computers fundamentally work with binary data (sequences of bytes). Buffers give you a way to read, write, and manipulate that raw binary data before converting it to or from other formats like strings, images, or network packets.
Think of a Buffer like a fixed-size container of raw bytes—similar to an array, but specifically designed for binary data rather than JavaScript values.
Why Buffers Matter
When you're working with streams (reading files, network communication, handling binary protocols), the data arrives as raw bytes. Before Node.js can do anything useful with it—like display text or process an image—it needs somewhere to temporarily store those bytes. That's what a Buffer does.
Buffers exist because JavaScript was originally designed for browsers and didn't have a native way to handle binary data efficiently. Node.js added Buffers to bridge this gap, especially for server-side tasks like file I/O and networking.
Key Characteristics
- Fixed size: Once created, a Buffer's length cannot change
- Raw bytes: Stores data in its binary form (0-255 per byte)
- Global: The
Bufferclass is available everywhere in Node.js without requiring a module - Bridge: Converts between binary data and JavaScript types (strings, arrays, etc.)
Key Takeaway: A Buffer is Node.js's specialized container for handling raw binary data in memory—essential for file operations, streams, and network communication where data arrives as bytes rather than text.