Skip to content

Instantly share code, notes, and snippets.

@leolanese
Created September 9, 2015 14:31
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 leolanese/36fe3879dd7b97c9f63f to your computer and use it in GitHub Desktop.
Save leolanese/36fe3879dd7b97c9f63f to your computer and use it in GitHub Desktop.
a betterTypeOf
var betterTypeOf = function(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
/*
betterTypeOf(null); // null
betterTypeOf(NaN); // number
betterTypeOf({a: 4}); //"object"
betterTypeOf([1, 2, 3]); //"array"
(function() {console.log(toType(arguments))})(); //arguments
betterTypeOf(new ReferenceError); //"error"
betterTypeOf(new Date); //"date"
betterTypeOf(/a-z/); //"regexp"
betterTypeOf(Math); //"math"
betterTypeOf(JSON); //"json"
betterTypeOf(new Number(4)); //"number"
betterTypeOf(new String("abc")); //"string"
betterTypeOf(new Boolean(true)); //"boolean"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment