Last active
August 7, 2020 10:42
-
-
Save yukal/a79fdf4ac404f7674b23fddb78337c6c to your computer and use it in GitHub Desktop.
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) { | |
const signature = Object.prototype.toString.call(obj); | |
return signature.slice(8,-1).toLowerCase(); | |
} | |
const cases = [ | |
// undefined | |
undefined, | |
// null | |
null, | |
// Boolean | |
false, | |
true, | |
// Number | |
NaN, | |
Infinity, | |
-Infinity, | |
-0, | |
-1, | |
// String | |
'', | |
// Symbol | |
Symbol('sym'), | |
// Object | |
(function(){}), | |
[], | |
{}, | |
]; | |
for (const val of cases) { | |
const typeName = getType(val); | |
typeName === 'string' | |
? console.log('%s\t"%s"', typeName, val) | |
: console.log('%s\t%s', typeName, val) | |
; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment