Skip to content

Instantly share code, notes, and snippets.

@mvsde
Created June 12, 2019 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvsde/20ad1c1d2972bdd2350ae9ecda80c20c to your computer and use it in GitHub Desktop.
Save mvsde/20ad1c1d2972bdd2350ae9ecda80c20c to your computer and use it in GitHub Desktop.
Get JavaScript data type
/**
* Get data type
* @see {@link https://gomakethings.com/a-better-way-to-create-an-immutable-copy-of-an-array-or-object-with-vanilla-js/}
*
* @param {} item Thing we want the type of
* @returns {String} Thing's type
*/
function getDataType (item) {
/**
* Unfortunately, `typeof` isn't that useful to detect types like `array`
* or `null`. This great workaround by Chris Ferdinandi greatly simplifies
* type checking by removing the need for a lot of chained conditionals
* (eg. `Array.isArray(item)` and `item === null`).
*/
return Object.prototype.toString.call(item)
.slice(8, -1)
.toLowerCase()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment