Buffers & Binary Data
The problem
JavaScript strings are built for text, but a lot of what a server handles — file contents, network packets, images — is raw binary data with no meaningful text encoding at all, and treating it as a string risks silent corruption at every encoding boundary.
Why now
Number systems and bases already established that everything is ultimately binary; JavaScript's ordinary values, though, are text/number abstractions sitting on top of that binary reality, hiding it. A Buffer is Node's way of working with the raw bytes directly, without the string layer's encoding assumptions getting in the way.
Mental model
A Buffer is a fixed-length array of raw bytes, each one just a number from 0–255 — no character encoding is implied until you explicitly ask for one ('interpret these bytes as UTF-8'). This is exactly the layer beneath strings-and-text's character-encoding discussion: a string is bytes plus a chosen interpretation, and a Buffer is the bytes alone, interpretation deferred or entirely skipped.
Requires
- Number Systems & BasesMathematical Thinking
A Buffer is literally an array of byte values (0–255), and working with it — reading specific byte offsets, interpreting multi-byte numbers — is direct positional/binary reasoning, not an abstraction over it.
- Types & ValuesProgramming Fundamentals
Understanding a Buffer requires clearly distinguishing it from a string type — same underlying bytes, fundamentally different type with different valid operations, which is exactly the type-as-a-set-of-valid-operations framing already established.
Used in
- Reading and writing binary files (images, executables, compressed archives)
- Low-level network protocol implementation, parsing raw packet bytes
- Cryptographic operations, which universally operate on raw bytes, not text
- Node's fs and net modules, which return Buffers by default for binary-safe data
Projects
- Read a small binary file (e.g. a PNG) into a Buffer and manually parse its fixed-format header bytes to extract width and height
- Demonstrate a real encoding bug by converting a Buffer containing multi-byte UTF-8 characters to a string using the wrong encoding, and explain the corruption
Examples
- Buffer.from('A').toString('hex') shows the raw byte 41 — the same underlying data string and Buffer types both represent, viewed at two different levels
- Slicing a Buffer at the wrong byte offset when parsing a binary file format silently produces garbage instead of an error, unlike a type mismatch in higher-level code
Resources
Mastery checklist
- I can explain the difference between a Buffer and a string in terms of encoding
- I can read specific byte offsets from a Buffer to parse a simple binary format
- I can convert between a Buffer and a string using an explicit encoding
- I can explain why binary file or network data should be handled as a Buffer, not a string, until explicitly decoded