Open In App

Node.js Buffer.isBuffer() Method

Improve
Improve
Like Article
Like
Save
Share
Report

Buffer is a temporary memory storage that stores the data when it is being moved from one place to another. It is like an array of integers. 

The Buffer.isBuffer() method checks whether the provided object is a buffer or not. 

Syntax:

Buffer.isBuffer( obj )

Parameters: This method accepts a single parameter obj which needs to be checked if it is a buffer or not. 

Return Value: This method returns a Boolean Value, true if the object is buffer, otherwise returns false

Example 1: The below example illustrates the use of Buffer.isBuffer() Method in Node.js: 

javascript




// Node.js program to demonstrate the
// Buffer.isBuffer() Method
 
const buffer = Buffer.from('GeeksForGeeks');
 
console.log(Buffer.isBuffer(buffer));


Output

true

Example 2: The below example illustrates the use of Buffer.isBuffer() Method in Node.js: 

javascript




// Node.js program to demonstrate the
// Buffer.isBuffer()) Method
 
const buf1 = "GeeksforGeeks";
 
console.log(Buffer.isBuffer(buf1));
 
const buf2 = new Buffer(4);
 
for (let i = 0; i < 4; i++) {
    buf2[i] = i + 97;
}
 
// Prints: abcd
// as 97, 98, 99, 100 are the ASCII
// values of these chars respectively
 
console.log(buf2.toString());
 
console.log(Buffer.isBuffer(buf2));
// Prints: true


Output: 

false
abcd
true

Note: The above program will compile and run by using the node index.js command. 

Reference: https://nodejs.org/api/buffer.html#buffer_class_method_buffer_isbuffer_obj


Last Updated : 11 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads