DEV Community

Discussion on: A better `typeof`

Collapse
 
siddharthshyniben profile image
Siddharth • Edited

Thanks!

Nice catch on the NaN. I think Object.is would fix it, right?

I think most people would prefer not having the full class so I set it to false

Collapse
 
supportic profile image
Supportic • Edited

I think most people would prefer not having the full class so I set it to false

Oh I gotcha, I first didn't understand what you meant to do with a non provided variable but it's actually undefined when not passed and therefore false. All good.

Looks good. Very useful! For testing purposes I renamed the function to showType() but if someone wants to see the differences here you go:

const main = () => {
  const tests = [
    ['an', 'array'],
    `the result is ${1 + 2}`,
    1n,
    new Date(),
    new Error('an error'),
    function () {},
    function* () {},
    /regex/gi,
    Symbol(),
    'not a number' / 2,
    document.querySelector('body'),
    1 / 0,
    null,
  ];

  for (const [i, test] of tests.entries()) {
    console.log(test);
    console.log('showType: ' + showType(test));
    console.log('showType fullClass: ' + showType(test, true));
    console.log('typeof: ' + typeof test);

    i < tests.length-1 ? console.log('========================') : null
  }
};
Enter fullscreen mode Exit fullscreen mode