Skip to content

Instantly share code, notes, and snippets.

@ryanmorr
Last active March 29, 2024 06:40
Show Gist options
  • Save ryanmorr/d5ef780734c4ba1cc5c05b5da5529dae to your computer and use it in GitHub Desktop.
Save ryanmorr/d5ef780734c4ba1cc5c05b5da5529dae to your computer and use it in GitHub Desktop.
Get the class of an object for type detection
function getClass(obj) {
return {}.toString.call(obj).slice(8, -1);
}
// Usage:
getClass([]); //=> "Array"
getClass('foo'); //=> "String"
getClass(123); //=> "Number"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment