Last active
August 29, 2015 13:56
-
-
Save bradoyler/9174519 to your computer and use it in GitHub Desktop.
JS console lesson : detect datatype
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 getType(value) | |
{ | |
var dtypes = [Function, RegExp, Number, String, Boolean, Object], x, len; | |
if (typeof value === "object" || typeof value === "function") { | |
for (x = 0, len = dtypes.length; x < len; x++) | |
{ | |
if (value instanceof dtypes[x]) | |
{ | |
return dtypes[x]; | |
} | |
} | |
} | |
return typeof value; | |
} | |
console.log(getType(12)); // Number | |
console.log(getType('w3c')); //String | |
console.log(getType(false)); // Boolean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment