Created
June 12, 2019 05:19
-
-
Save mvsde/20ad1c1d2972bdd2350ae9ecda80c20c to your computer and use it in GitHub Desktop.
Get JavaScript data type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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