Last active
June 3, 2021 17:49
-
-
Save kdubbels/7406a8dbb244487b8b1562b0f866ed59 to your computer and use it in GitHub Desktop.
Get the type of a value from its prototype
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
function getType(obj) { | |
// slice first 8 characters of, e.g., "[object String]" or "[object Object]" to get type | |
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase(); | |
} | |
getType("foo"); // "string" | |
getType([]); // "array" | |
getType({}); // "object" | |
getType(new Set()); // "set" | |
getType(true); // "boolean" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment