JSFiddle - React, Tailwind, and code Playground
JavaScript
var TypeOf = function ( thing ) {
var typeOfThing = typeof thing;
if ( typeOfThing === 'object' ) {
typeOfThing = Object.prototype.toString.call(thing);
if ( typeOfThing === '[object Object]') {
if ( thing.constructor.name ) {
return thing.constructor.name;
} else if ( thing.constructor.toString().charAt(0) === '[' ) {
typeOfThing = typeOfThing.substring(8,typeOfThing.length - 1);
} else {
typeOfThing = thing.constructor.toString().match(/function\s*(\w+)/);
if ( typeOfThing ) {
return typeOfThing[1];
} else {
return 'Function';
}
}
} else {
typeOfThing = typeOfThing.substring(8,typeOfThing.length - 1);
}
}
return typeOfThing.charAt(0).toUpperCase() + typeOfThing.slice(1);
}
console.log('Type of Object: ' + TypeOf({}));
console.log('Type of Null: ' + TypeOf(null));
console.log('Type of Function: ' + TypeOf(function() {}));
console.log('Type of Number #1: ' + TypeOf(NaN));
console.log('Type of Number #2: ' + TypeOf(new Number));
console.log('Type of String #1: ' + TypeOf(" "));
console.log('Type of String #2: ' + TypeOf(new String));
console.log('Type of Undefined: ' + TypeOf(undefined));
console.log('Type of Boolean: ' + TypeOf(true));
console.log('Type of Array: ' + TypeOf([]));
console.log('Type of Date: ' + TypeOf(new Date));
console.log('Type of RegExp: ' + TypeOf(/.+/));
function customClass () {};
var cc = new customClass();
console.log('Type of Custom Class: ' + TypeOf(cc));
var a = function () {};
var b = new a();
console.log('Type of Anonymous Function: ' + TypeOf(b));