Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Last active August 29, 2015 13:56
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 bradoyler/9174519 to your computer and use it in GitHub Desktop.
Save bradoyler/9174519 to your computer and use it in GitHub Desktop.
JS console lesson : detect datatype
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