codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🟢

Node.js

10 / 63 topics
6Node.js File System7HTTP Module8Node.js Events9Streams10Buffers11Path Module12OS Module
Tutorials/Node.js/Buffers
🟢Node.js

Buffers

Updated 2026-05-15
10 min read

Buffers

Introduction

In the world of Node.js, buffers are a fundamental concept that allow you to work with binary data. They provide an efficient way to handle raw data in memory, making them essential for tasks such as reading and writing files, handling network communications, and more.

Buffers are instances of the Buffer class in Node.js, which is part of the core modules. Buffers are similar to arrays of integers but represent sequences of binary data rather than text strings. Each element in a buffer corresponds to a single byte of memory.

Concept

What Are Buffers?

A buffer is essentially a chunk of raw binary data stored in memory. In Node.js, buffers are used to handle low-level operations involving binary data. They can be created using various methods and manipulated using a range of built-in functions.

Key Characteristics of Buffers:

  1. Fixed Size: Once a buffer is created, its size cannot be changed.
  2. Binary Data: Buffers store raw binary data, not text strings.
  3. Efficient Memory Management: They are optimized for performance and memory usage when dealing with large amounts of binary data.

Creating Buffers

There are several ways to create buffers in Node.js:

  1. Using Buffer.alloc(size):

    • Allocates a buffer of the specified size (in bytes).
    • The buffer is initialized with zeros.
  2. Using Buffer.from(array):

    • Creates a buffer from an array of integers (0 to 255).
  3. Using Buffer.from(string, encoding):

    • Creates a buffer containing the encoded byte representation of the given string.

Example: Creating Buffers

Let's explore how to create buffers using these methods:

JavaScript
1// Create a buffer of size 10 initialized with zeros
2const buf1 = Buffer.alloc(10);
3console.log(buf1); // Output: <Buffer 00 00 00 00 00 00 00 00 00 00>
4
5// Create a buffer from an array of integers
6const buf2 = Buffer.from([1, 2, 3]);
7console.log(buf2); // Output: <Buffer 01 02 03>
8
9// Create a buffer from a string with UTF-8 encoding
10const buf3 = Buffer.from('Hello', 'utf-8');
11console.log(buf3); // Output: <Buffer 48 65 6c 6c 6f>

Examples

Reading and Writing to Buffers

Buffers can be read from and written to using various methods. Here are some common operations:

  1. Writing to a Buffer:

    • Use buffer.write(string, offset, length, encoding) to write a string to the buffer.
  2. Reading from a Buffer:

    • Use buffer.toString(encoding, start, end) to convert the buffer's content back to a string.

Example: Reading and Writing

Let's see how to read from and write to a buffer:

JavaScript
1const buf = Buffer.alloc(10);
2
3// Write a string to the buffer
4buf.write('Hello', 0, 5, 'utf-8');
5console.log(buf.toString()); // Output: Hello
6
7// Write another string at offset 6
8buf.write(' World', 6, 6, 'utf-8');
9console.log(buf.toString()); // Output: Hello World

Buffer Concatenation

Sometimes you need to concatenate multiple buffers into a single buffer. Node.js provides the Buffer.concat() method for this purpose.

Example: Concatenating Buffers

Here's how you can concatenate two buffers:

JavaScript
1const buf1 = Buffer.from('Hello');
2const buf2 = Buffer.from(' World');
3
4// Concatenate the buffers
5const combinedBuf = Buffer.concat([buf1, buf2]);
6console.log(combinedBuf.toString()); // Output: Hello World

What's Next?

Now that you have a good understanding of buffers in Node.js, it's time to explore another essential core module: the Path Module. The Path Module provides utilities for working with file and directory paths, making it easier to handle file system operations.

Stay tuned for more tutorials on Node.js core modules!


Feel free to experiment with buffers using the examples provided. Buffers are a powerful tool in your Node.js toolkit, allowing you to efficiently manage binary data in your applications.


PreviousStreamsNext Path Module

Recommended Gear

StreamsPath Module