Skip to content

Instantly share code, notes, and snippets.

@kdubbels
Last active June 3, 2021 17:49
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 kdubbels/7406a8dbb244487b8b1562b0f866ed59 to your computer and use it in GitHub Desktop.
Save kdubbels/7406a8dbb244487b8b1562b0f866ed59 to your computer and use it in GitHub Desktop.
Get the type of a value from its prototype
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