Advertisement
Intermediate Node.js, Core Node.js Modules

What does the node:util.types module provide, and how does it differ from JavaScript's built-in typeof operator?

Short answer

util.types provides precise type-checking functions (isArrayBuffer, isDate, isPromise, etc.) correctly distinguishing built-in object types -- something typeof can't do, since it returns 'object' for arrays, dates, regexes, and plain objects alike.

Save Node.js mock interview
Advertisement
AModel answer
util.types provides precise type-checking functions (isArrayBuffer, isDate, isPromise, etc.) correctly distinguishing built-in object types -- something typeof can't do, since it returns 'object' for arrays, dates, regexes, and plain objects alike.
util.types.isDate(new Date()); // true
util.types.isPromise(Promise.resolve()); // true
Real-world example
A serialization library uses util.types.isDate() and isRegExp() to correctly detect these types during serialization, impossible to do reliably with typeof alone.
Likely follow-ups
  • How does util.types.isNativeError() differ from checking instanceof Error?
  • What's the relationship between util.types and Object.prototype.toString.call()?
Related topics