DEV Community

Discussion on: Detecting Object vs Array in JavaScript by example

Collapse
 
0x04 profile image
Oliver Kühn • Edited

Had to mention a very simple way to detect the real data class:

const is = (object, isType = null) => {
  var type = Object.prototype.toString.call(object).slice(8, -1);
  return (isType) ? type === isType : type;
}

is([1, 2, 3], 'Array') // true
is(document) // 'HTMLDocument'

Source: Using toString() to detect object class