Buffer.isBuffer()

Reports whether a value is a Buffer, returning false for strings, Uint8Arrays and plain objects.

Signature
Buffer.isBuffer(obj)
  • Added in Node.js v0.1.101

Parameters

ParameterTypeDescription
$objObjectThe value to test. Any value is accepted, including null and undefined.

Return value

booleantrue if obj is a Buffer, false otherwise.

Errors

Never throws; any value, including null and undefined, returns a boolean.

Buffer.isBuffer() is the type guard for functions that accept either text or bytes. It is safe on any input — null and undefined return false rather than throwing — which makes it usable as the first line of an argument check.

What it does not match

A plain Uint8Array returns false, even though Buffer extends Uint8Array. That trips people up when data arrives from fetch, crypto.subtle or the TextEncoder, all of which produce Uint8Array. If your function can work on either, test ArrayBuffer.isView(value) or value instanceof Uint8Array instead, and only use isBuffer when you specifically need Node's extra methods.

An object of the shape { type: 'Buffer', data: [...] } — what JSON.parse gives back after a Buffer crossed a JSON boundary — is also false. It looks like a Buffer in a log line but it is a plain object.

Versus instanceof

isBuffer is more reliable than value instanceof Buffer because it does not depend on the prototype identity, which can differ across realms such as a vm context or two copies of a package in node_modules.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Branch in an API that accepts either a string path or raw bytes.
  • Validate a function argument before calling Buffer-only methods on it.
  • Decide whether to encode a value or pass it through untouched.
  • Detect that a Buffer was flattened into a plain object by JSON round-tripping.

Frequently asked questions

Why does Buffer.isBuffer return false for a Uint8Array?

Because Buffer is a subclass of Uint8Array, so every Buffer is a Uint8Array but not the reverse. Use ArrayBuffer.isView or instanceof Uint8Array when you want to accept both.

Should I use Buffer.isBuffer or instanceof Buffer?

Use Buffer.isBuffer, because it checks an internal marker rather than prototype identity and keeps working across vm contexts and duplicated module copies. instanceof can return false for a genuine Buffer created in a different realm.

How do I check if a value is a Buffer without throwing on null?

Buffer.isBuffer never throws, so Buffer.isBuffer(null) simply returns false. No guard around the call is needed.

More examples

Normalising an argument that may be text or bytes

The isBuffer branch is a fast path that avoids a copy when the caller already handed you bytes. The ArrayBuffer.isView branch is what catches the Uint8Array that isBuffer rejects, and passing byteOffset and byteLength keeps a windowed typed array from expanding into its whole backing store.

Example · javascript
function toBuffer(input, encoding = 'utf8') {
  if (Buffer.isBuffer(input)) return input;                 // already bytes
  if (typeof input === 'string') return Buffer.from(input, encoding);
  if (ArrayBuffer.isView(input)) return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
  if (input instanceof ArrayBuffer) return Buffer.from(input);
  throw new TypeError(`cannot convert ${typeof input} to a Buffer`);
}

console.log(toBuffer('hi').toString('hex'));                       // 6869
console.log(toBuffer(new TextEncoder().encode('hi')).toString());  // hi
console.log(toBuffer(Buffer.from('hi')).toString());               // hi

isBuffer versus instanceof across realms

isBuffer uses an internal marker rather than the prototype chain, so it keeps working when two copies of a module or a separate vm context are involved. The second half shows the case you will actually hit: a Buffer that went through JSON is a plain object until you rebuild it with Buffer.from.

Example · javascript
const vm = require('node:vm');

const other = vm.runInNewContext('Buffer.from("abc")', { Buffer });
console.log(Buffer.isBuffer(other));    // true
console.log(other instanceof Buffer);   // true here, but not guaranteed

// The check that fails more often in practice:
const fromJson = JSON.parse(JSON.stringify(Buffer.from('abc')));
console.log(Buffer.isBuffer(fromJson));            // false
console.log(Array.isArray(fromJson.data));         // true
console.log(Buffer.isBuffer(Buffer.from(fromJson.data))); // true

Discussion

  • Be the first to comment on this lesson.