How to get type of a variable in string in JavaScript

In this Article we will go through how to get type of a variable in string only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const getTypeOf = obj => Object.prototype.toString.call(obj).match(/\[object (.*)\]/)[1];

#Example

getTypeOf('hello world');           // String
getTypeOf(1000);                    // Number
getTypeOf(Infinity);                // Number
getTypeOf(true);                    // Boolean
getTypeOf(Symbol());                // Symbol
getTypeOf(null);                    // Null
getTypeOf(undefined);               // Undefined
getTypeOf({});                      // Object
getTypeOf([]);                      // Array
getTypeOf(/[a-z]/g);                // RegExp
getTypeOf(new Date(2021));          // Date
getTypeOf(new Error());             // Error
getTypeOf(function() {});           // Function
getTypeOf((a, b) => a + b);         // Function
getTypeOf(async () => {});          // AsyncFunction
getTypeOf(document);                // HTMLDocument
\n