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 )
typeOfThing = 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+)/)[1];
} else {
typeOfThing = typeOfThing.substring(8,typeOfThing.length - 1);
}
return typeOfThing.toLowerCase();
} else {
return typeOfThing;
}
}
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(/.+/));
// This doesn't work -> constructor is: function () {}
// var me = function () {};
function me () {};
var you = new me();
console.log('Type of you: ' + TypeOf(you));