Node.js | Buffer.isBuffer() Method

Buffer is a temporary memory storage which 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 single parameter obj which need 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.

Below examples illustrate the use of Buffer.isBuffer() Method in Node.js:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output

true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the  
// Buffer.isBuffer()) Method
  
var buf1 = "GeeksforGeeks"
  
console.log(Buffer.isBuffer(buf1));
  
var buf2 = new Buffer(4);
  
for (var 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

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.