What a Buffer Is
A Buffer represents a fixed-length chunk of raw binary data, similar to an array of bytes. It exists because JavaScript strings are built for text, not binary data — reading an image file or a network packet needs something else.
Creating Buffers
Console Output
Click “Run” to see the console output here.
Converting Between Buffers and Strings
Console Output
Click “Run” to see the console output here.
Where Buffers Show Up
| Context | Example |
|---|---|
| File system | fs.readFile() without an encoding returns a Buffer |
| Streams | Readable streams emit Buffer chunks by default |
| Networking | Raw TCP/HTTP data arrives as Buffers |
| Cryptography | Hashes and encrypted data are commonly Buffers |
Buffer Length is Bytes, Not Characters
buf.length reports the number of bytes, not characters — a string with multi-byte UTF-8 characters (like emoji or accented letters) will have a buffer length larger than the visible character count.
Best Practice
You rarely need to create Buffers manually in everyday application code — most high-level APIs (fs.readFile with an encoding, HTTP body parsers) hand you a string or parsed object already. Reach for Buffer directly when working with genuinely binary data.