Last active
March 29, 2024 06:40
-
-
Save ryanmorr/d5ef780734c4ba1cc5c05b5da5529dae to your computer and use it in GitHub Desktop.
Get the class of an object for type detection
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 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