JSFiddle - React, Tailwind, and code Playground
by petroveg
HTML
<!---->
JavaScript
console.log(getType());
console.log(getType(null));
console.log(getType(''));
console.log(getType(3));
console.log(getType([]));
console.log(getType(Object.create(Array.prototype)));
console.log(getType(Object.create(Array)));
console.log(getType({}));
console.log(getType(new Date()));
console.log(getType(/a/));
console.log(getType(Object.create(null)));
console.log(getType(document));
console.log(getType(document.documentElement));
console.log(getType(document.documentElement.previousSibling));
console.log(getType(document.createDocumentFragment()));
console.log(getType(document.body.firstChild));
console.log(getType(document.body.firstChild.nextSibling));
try {
console.log(getType(Symbol()));
} catch (z) {
console.log('Not Supported ES6: Symbol');
}
try {
x = y;
} catch (z) {
console.log(getType(z));
}
function getType (e) {
var nodeType = [
'Element',
'Attribute',
'Text Node',
'CDATA Section',
'Entity Reference',
'Entity',
'Processing Instruction',
'Comment',
'Document',
'Document Type',
'Document Fragment',
'Notation'
],
dataType = [
'Error',
'Symbol',
'Date',
'RegExp',
'Function',
'Boolean',
'Number',
'Array',
'String'
];
if (e === undefined || e === null) {
return e;
} else {
var type = typeof e;
if (type != 'object') {
return type.slice(0, 1).toUpperCase() + type.slice(1);
} else {
var name = Object.prototype.toString.call(e);
if (name == '[object Object]') {
for (var i = dataType.length; i--;) {
if (window[dataType[i]] && window[dataType[i]].prototype.isPrototypeOf(e)) {
return dataType[i];
}
}
return 'Object';
} else {
type = name.match(/(\w+)[^\w]*$/);
if (type) {
if (dataType.indexOf(type[1]) >= 0) {
return type[1];
} else if (e.nodeType) {
return nodeType[e.nodeType - 1];
}
} else {
return 'Unknown Type';
}
}
}
}
}