A better `typeof`

The typeof operator is a really useful one but it has a few pitfalls:

typeof [“an”, “array”] // object
typeof /regex/g // object
typeof null // object
typeof NaN // number
typeof Number(‘I am not a number!’) // number

Ok, that’s a lot of pitfa…


This content originally appeared on DEV Community and was authored by Siddharth

The typeof operator is a really useful one but it has a few pitfalls:

typeof ["an", "array"] // object
typeof /regex/g // object
typeof null // object
typeof NaN // number
typeof Number('I am not a number!') // number

Ok, that's a lot of pitfalls;

But there is a way to get more detailed types using Object.prototype.toString.call() on a value:

// This statement basically means: "Call the toString method of the object prototype on whatever value you like"
Object.prototype.toString.call({ object: "true" }) // the infamous [object Object]
Object.prototype.toString.call(["an", "array"]) // [object Array]
Object.prototype.toString.call("a string") // [object String]
Object.prototype.toString.call(1n) // [object Bigint]
Object.prototype.toString.call(new Date()) // [object Date] really
Object.prototype.toString.call(new Error("an error")) // [object Error]
Object.prototype.toString.call(function () {}) // [object Function]
Object.prototype.toString.call(function* () {}) // [object GeneratorFunction]
Object.prototype.toString.call(/regex/gi) // [object RegExp]
Object.prototype.toString.call(Symbol()) // [object Symbol]
Object.prototype.toString.call(NaN) // it's not perfect: [object Number]

Of course, this could be made a function (with a few finishing touches from here)

  function type(obj, showFullClass) {

    // Whether to return the whole type
    if (showFullClass && typeof obj === "object") {
        return Object.prototype.toString.call(obj);
    }

    if (obj == null) { return (obj + '').toLowerCase(); } // implicit toString() conversion

    if (isNaN(+obj)) return "nan";

    var deepType = Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();
    if (deepType === 'generatorfunction') { return 'function' }

    // Prevent overspecificity (for example, [object HTMLDivElement], etc).
    // Account for functionish Regexp (Android <=2.3), functionish <object> element (Chrome <=57, Firefox <=52), etc.
    // String.prototype.match is universally supported.

    return deepType.match(/^(array|bigint|date|error|function|generator|regexp|symbol)$/) ? deepType :
       (typeof obj === 'object' || typeof obj === 'function') ? 'object' : typeof obj;
  }


This content originally appeared on DEV Community and was authored by Siddharth


Print Share Comment Cite Upload Translate Updates
APA

Siddharth | Sciencx (2021-05-23T13:49:41+00:00) A better `typeof`. Retrieved from https://www.scien.cx/2021/05/23/a-better-typeof/

MLA
" » A better `typeof`." Siddharth | Sciencx - Sunday May 23, 2021, https://www.scien.cx/2021/05/23/a-better-typeof/
HARVARD
Siddharth | Sciencx Sunday May 23, 2021 » A better `typeof`., viewed ,<https://www.scien.cx/2021/05/23/a-better-typeof/>
VANCOUVER
Siddharth | Sciencx - » A better `typeof`. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/23/a-better-typeof/
CHICAGO
" » A better `typeof`." Siddharth | Sciencx - Accessed . https://www.scien.cx/2021/05/23/a-better-typeof/
IEEE
" » A better `typeof`." Siddharth | Sciencx [Online]. Available: https://www.scien.cx/2021/05/23/a-better-typeof/. [Accessed: ]
rf:citation
» A better `typeof` | Siddharth | Sciencx | https://www.scien.cx/2021/05/23/a-better-typeof/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.