Skip to content

Instantly share code, notes, and snippets.

@razouck
Last active November 18, 2017 16:33
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 razouck/b1c111f9d904452b11ff0b1a8d93fa58 to your computer and use it in GitHub Desktop.
Save razouck/b1c111f9d904452b11ff0b1a8d93fa58 to your computer and use it in GitHub Desktop.
Returns the type/tag of a given value.
function tagof (value) {
return value != value ? 'NaN' :
Object.prototype.toString.call(value)
.match(/\[object (\w+)\]/)[1]
.toLowerCase();
}
[
[],
false,
new Date(),
Error(),
function () {},
1 / 'zero',
null,
0,
{},
RegExp(/a/),
'a',
Symbol(),
undefined
]
.forEach(value => console.log(tagof(value)));
// That's all there is to it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment