Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Node.js BuffersNode.js Buffers

Node.js Buffers

Posted on October 3, 2025October 3, 2025 By Rishabh saini No Comments on Node.js Buffers

Node.js Buffers

When working with raw binary data in Node.js, the Buffer class plays a key role. Unlike arrays, Buffers represent raw memory allocations outside the V8 heap, making them essential for handling binary data in operations such as file system interactions and TCP streams.

Introduction to Applied AI:–Click Here

JavaScript on its own does not handle binary data efficiently, which is why the Buffer class was introduced. The good part is that Buffer is a global class, so you can use it directly without importing the buffer module.

Creating Buffers in Node.js

There are several ways to create a Buffer, but the following three are the most common:

Data Science Tutorial:-Click Here

  1. Creating an uninitialized buffer

var buf = new Buffer(10);

This creates a buffer of 10 octets.

  1. Creating a buffer from an array

var buf = new Buffer([10, 20, 30, 40, 50]);

This initializes a buffer with the values provided in the array.

Download New Real Time Projects :–Click here

  1. Creating a buffer from a string

var buf = new Buffer("Simply Easy Learning", "utf-8");

This creates a buffer from a given string, with an optional encoding (default is UTF-8).

Writing to Buffers

To write data into a buffer, Node.js provides the write() method.

Syntax:

buf.write(string[, offset][, length][, encoding])

  • string: Data to be written into the buffer.
  • offset: Position to start writing. Default is 0.
  • length: Number of bytes to write. Default is the buffer’s length.
  • encoding: Encoding type. Default is utf8.

Return Value:
Returns the number of octets written. If the buffer doesn’t have enough space, only part of the string will be written.

Machine Learning Tutorial:–Click Here

Example:
File: main.js

buf = new Buffer(256);  
len = buf.write("Simply Easy Learning");  
console.log("Octets written : " + len);

Run the file using:

node main.js

Output:

Octets written : 20

Reading from Buffers

To read data from a buffer, we use the toString() method.

Complete Advance AI topics:- CLICK HERE

Syntax:

buf.toString([encoding][, start][, end])

  • encoding: Character encoding (default is utf8).
  • start: Starting index (default is 0).
  • end: Ending index (default is the complete buffer).

Return Value:
Returns a decoded string from the buffer data.

Example:
File: main.js

buf = new Buffer(26);  
for (var i = 0; i < 26; i++) {  
  buf[i] = i + 97;  
}  

console.log(buf.toString('ascii'));        // abcdefghijklmnopqrstuvwxyz  
console.log(buf.toString('ascii', 0, 5));  // abcde  
console.log(buf.toString('utf8', 0, 5));   // abcde  
console.log(buf.toString(undefined, 0, 5));// defaults to utf8 -> abcde

Run the file using:

node main.js

Output:

abcdefghijklmnopqrstuvwxyz
abcde
abcde
abcde

Would you like me to also add a section on updating/deprecations (like using Buffer.alloc and Buffer.from instead of new Buffer()) since new Buffer() is deprecated in modern Node.js? That will make the article more up-to-date and professional for UpdateGadh.

Deep Learning Tutorial:– Click Here
Complete Python Course with Advance topics:-Click Here
SQL Tutorial :–Click Here


node js buffer example
node js buffers w3schools
streams and buffers in node js
how to create buffer in node js
buffer – npm
nodejs buffer to string
nodejs buffer to base64
buffer from base64

    Post Views: 262
    Node.js Tutorial Tags:buffer in nodejs, buffer nodejs, buffer tutorial in nodejs, how buffers work in node.js, how buffers work in nodejs, how to use buffers in node.js, node.js buffers, node.js buffers explained, nodejs buffer, nodejs buffer class, nodejs buffer example, nodejs buffer module, nodejs buffer to string, nodejs buffer tutorial, nodejs buffer video, nodejs buffers, nodejs buffers explained, nodejs streams and buffers, nodejs write buffer to file, streams and buffers in nodejs, streams vs buffers nodejs

    Post navigation

    Previous Post: Best Online Grocery Shop Using Python & Django
    Next Post: Best Library Management System Using PHP and MySQL

    More Related Articles

    Nodejs REPL Nodejs REPL Node.js Tutorial
    Node.js MongoDB Create Collection Node.js MongoDB Create Collection Node.js Tutorial
    Node.js Callbacks Node.js Callbacks Node.js Tutorial

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    You may also like

    1. Install Nodejs on Windows
    2. Node.js Command Line Options
    3. Node.js Assertion Testing
    4. Node.js Events
    5. Node.js MySQL Delete Records
    6. Node.js Create Connection with MongoDB

    Most Viewed Posts

    1. Top Large Language Models in 2025
    2. Online Shopping System using PHP, MySQL with Free Source Code
    3. login form in php and mysql , Step-by-Step with Free Source Code
    4. Flipkart Clone using PHP And MYSQL Free Source Code
    5. News Portal Project in PHP and MySql Free Source Code
    6. User Login & Registration System Using PHP and MySQL Free Code
    7. Top 10 Final Year Project Ideas in Python
    8. Online Bike Rental Management System Using PHP and MySQL
    9. E learning Website in php with Free source code
    10. E-Commerce Website Project in Java Servlets (JSP)
    • AI
    • ASP.NET
    • Blockchain
    • ChatCPT
    • code Snippets
    • Collage Projects
    • Data Science Project
    • Data Science Tutorial
    • DBMS Tutorial
    • Deep Learning Tutorial
    • Final Year Projects
    • Free Projects
    • How to
    • html
    • Interview Question
    • Java Notes
    • Java Project
    • Java Script Notes
    • JAVASCRIPT
    • Javascript Project
    • JSP JAVA(J2EE)
    • Machine Learning Project
    • Machine Learning Tutorial
    • MySQL Tutorial
    • Node.js Tutorial
    • PHP Project
    • Portfolio
    • Python
    • Python Interview Question
    • Python Projects
    • PythonFreeProject
    • React Free Project
    • React Projects
    • Spring boot
    • SQL Tutorial
    • TOP 10
    • Uncategorized
    • Online Examination System in PHP with Source Code
    • AI Chatbot for College and Hospital
    • Job Portal Web Application in PHP MySQL
    • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
    • Online Job Portal System in JSP Servlet MySQL

    Most Viewed Posts

    • Top Large Language Models in 2025 (8,613)
    • Online Shopping System using PHP, MySQL with Free Source Code (5,215)
    • login form in php and mysql , Step-by-Step with Free Source Code (4,867)

    Copyright © 2026 UpdateGadh.

    Powered by PressBook Green WordPress theme