-
-
Save axelav/15ecc2994190f235cc86010c01f2f063 to your computer and use it in GitHub Desktop.
getType
This file contains hidden or 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
function getType (value) { | |
let type = typeof value; | |
if (type === 'object') { | |
return value ? Object.prototype.toString.call(value).slice(8, -1) : 'null'; | |
} | |
return type; | |
} | |
[NaN, 0, 1, Infinity, // numbers | |
null, undefined, false, 'str', // other primitives | |
{}, [], new Map, new WeakSet(), // containers | |
/regex/, new Date(), // other custom objects | |
window, navigator, // native objects | |
function () {}, (() => {}), atob,// functions | |
Symbol() // symbol | |
].map(getType); | |
// and the result is | |
[ | |
"number", "number", "number", "number", // numbers | |
"null", "undefined", "boolean", "string",// other primitives | |
"Object", "Array", "Map", "WeakSet", // containers | |
"RegExp", "Date", // other custom objects | |
"Window", "Navigator", // native objects | |
"function", "function", "function", // functions | |
"symbol" // symbol | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment