Created
October 17, 2014 19:24
-
-
Save cristianobecker/171c47cd7f04f8f74b4a to your computer and use it in GitHub Desktop.
Get the type of any object in JavaScript
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 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