DEV Community

Discussion on: A better `typeof`

supportic profile image
Supportic • Edited on

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}`,
    new Date(),
    new Error('an error'),
    function () {},
    function* () {},
    'not a number' / 2,
    1 / 0,

  for (const [i, test] of tests.entries()) {
    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