Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djD-REK/4623f11019f77a8290145b77523b0e86 to your computer and use it in GitHub Desktop.
Save djD-REK/4623f11019f77a8290145b77523b0e86 to your computer and use it in GitHub Desktop.
// Object.prototype.toString.call() will check the type of any primitive or object in JavaScript:
console.log(Object.prototype.toString.call(new Date())) // [object Date]
console.log(Object.prototype.toString.call([])) // [object Array]
console.log(Object.prototype.toString.call(true)) // [object Boolean]
console.log(Object.prototype.toString.call(function () {})) // [object Function]
console.log(Object.prototype.toString.call((x => x))) // [object Function]
console.log(Object.prototype.toString.call(null)) // [object Null]
console.log(Object.prototype.toString.call(37)) // [object Number]
console.log(Object.prototype.toString.call(NaN)) // [object Number]
console.log(Object.prototype.toString.call(Infinity)) // [object Number]
console.log(Object.prototype.toString.call(-0)) // [object Number]
console.log(Object.prototype.toString.call({})) // [object Object]
console.log(Object.prototype.toString.call(/someRegularExpression/i)) // [object RegExp]
console.log(Object.prototype.toString.call("")) // [object String]
console.log(Object.prototype.toString.call(undefined)) // [object Undefined]
console.log(Object.prototype.toString.call()) // [object Undefined]
// Compare to the console.log(typeof keyword, which will return "object" for null or any object, including arrays:
console.log(typeof new Date()) // "object"
console.log(typeof []) // "object"
console.log(typeof true) // "boolean"
console.log(typeof function () {}) // "function"
console.log(typeof (x => x)) // "function"
console.log(typeof null) // "object"
console.log(typeof 37) // "number"
console.log(typeof NaN) // "number"
console.log(typeof Infinity) // "number"
console.log(typeof -0) // "number"
console.log(typeof {}) // "object"
console.log(typeof /someRegularExpression/i) // "object"
console.log(typeof "") // "string"
console.log(typeof undefined) // "undefined"
console.log(typeof undeclaredVariable) // "undefined"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment