Skip to content

Instantly share code, notes, and snippets.

@cristianobecker
Created October 17, 2014 19:24
Show Gist options
  • Save cristianobecker/171c47cd7f04f8f74b4a to your computer and use it in GitHub Desktop.
Save cristianobecker/171c47cd7f04f8f74b4a to your computer and use it in GitHub Desktop.
Get the type of any object in JavaScript
function type(e) {
var t = Object.prototype.toString.call(e),
r = /\[object (\w+)\]/i.exec(t);
return r && r[1].toLowerCase();
}
[1,"2", true, null, [], {}, function() {}, undefined].forEach(function(e){
console.log(type(e))
});
//number
//string
//boolean
//null
//array
//object
//function
//undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment