How to use Buffer.isBuffer() in Node.js

A buffer is an array-like object that stores a fixed-length sequence of bytes.

The isBuffer() method belongs to the Buffer class and tests whether an object is a buffer or not.

Prototype

Parameters

The Buffer.isBuffer() function accepts an object as a single parameter.

Return value

The Buffer.isBuffer() function returns true if the given object is a buffer and returns false if the given object is not a buffer.

Example

The following example demonstrates how to use the Buffer.isBuffer() function in Node.js.

// initialize buffer
var buf = Buffer.from([11,22,33,44])
var buf1 = Buffer.alloc(9)
// initialize list
var buf2 = [11,22,33,44]
// initialize buffer
var buf3 = Buffer.from('Educative.io')
// initialize string
var buf4 = "Educative.io"
console.log(Buffer.isBuffer(buf))
console.log(Buffer.isBuffer(buf1))
console.log(Buffer.isBuffer(buf2))
console.log(Buffer.isBuffer(buf3))
console.log(Buffer.isBuffer(buf4))

This example creates the buffers through the Buffer.from() and Buffer.alloc() functions, which store the inputs as bytes in the buffer. The example tests the isBuffer() function on a string, a list, and buffers.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved