Node.js Internals6h estimated

Buffers & Binary Data

Difficulty
Importance

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

Used in

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