Skip to content

Instantly share code, notes, and snippets.

@yukal
Last active August 7, 2020 10:42
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 yukal/a79fdf4ac404f7674b23fddb78337c6c to your computer and use it in GitHub Desktop.
Save yukal/a79fdf4ac404f7674b23fddb78337c6c to your computer and use it in GitHub Desktop.
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