What is the Node.js Buffer.isBuffer() method?

A Buffer is a temporary memory that holds data until it is consumed. A Buffer represents a fixed-size chunk of memorycan’t be resized allocated outside of the V8 JavaScript engine.

You can think of a Buffer like an array of integers that each represent a byte of data.

The Buffer.isBuffer() method of the Buffer object is used to check if an object is a Buffer or not.

isBuffer is implemented by the Node.js Buffer class.

Buffer and its methods do not need to be imported, as Buffer is a global class.

Syntax

Buffer.isBuffer([object])

Parameters

  • [object]: A required parameter that is the object to check if it is a Buffer or not.

Return value

isBuffer() returns a Boolean value that can be true or false. The method returns true if [object] is a Buffer; otherwise, it returns false.

Example

In the example below, we test different objects to see if they are Buffers.

// create a real Buffer object
const buf = Buffer.from("A buffer")
// creating other objcts
const name = "Theodore"
const ourObj = {}
const ourArr = []
// Checking to see if they are buffers
console.log(Buffer.isBuffer(name)) // false
console.log(Buffer.isBuffer(ourObj)) // false
console.log(Buffer.isBuffer(ourArr)) // false
console.log(Buffer.isBuffer(buf)) // true

In the code above, ourObj is not a Buffer because it is just a plain Javascript object. The array ourArr is also just a Javascript array object. Likewise, the string name is just a string and not a Buffer instance.

As we have seen, the isBuffer() method is used to check if an object is actually a Buffer.

Free Resources