JSFiddle - React, Tailwind, and code Playground
JavaScript
function whatIsType(variable) {
var type = Object.prototype.toString.call(variable);
var hash = {
'[object Function]': "Function",
'[object Date]': "Date",
'[object Error]': "Error",
'[object RegExp]': "RegExp",
'[object Array]': "Array",
'[object String]': "String",
'[object Boolean]': "Boolean",
'[object Number]': "Number",
'[object Null]': "Null",
'[object Undefined]': "Undefined"
}
if (type in hash) {
return hash[type];
} else {
if ('length' in variable) {
for (var i = 0; i < length - 1; i++) {
if (!(i in variable)) {
return "Object";
}
}
return "Array-like"
}
return "Object";
}
}
console.log(whatIsType()); //Undefind
console.log(whatIsType(true));
console.log(whatIsType(null));
console.log(whatIsType(1));
console.log(whatIsType("String"));
console.log(whatIsType(function() {}));
console.log(whatIsType([]));
//console.log(whatIsType(arguments)); //Array-like
console.log(whatIsType(new Object));
function SomeClass() { };
console.log(whatIsType(new SomeClass));
var arrayLike = { 0: 'a', 1: 'b', 2: 'c', length: 3 };
console.log(whatIsType(arrayLike));