question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: `no-typeof-undefined`

See original GitHub issue

Less code written

Fail

typeof foo === 'undefined'

Pass

foo === undefined

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
fiskercommented, Aug 18, 2021

I don’t think no-undef is safe, for example, when you write code for browser, the following won’t report, because AbortController is considered “defined” as globals.

if (AbortController !== undefined) {}

But when you run this code in old browsers don’t support AbortController , it will throw.

if (typeof AbortController !== 'undefined') {}

This won’t.

Of cause, 'AbortController' in globalThis or globalThis.AbortController are safer. But nothing wrong to use typeof.

5reactions
fiskercommented, Aug 18, 2021

When it’s undefined variable, foo === undefiend will throw

foo === undefined
VM227:1 Uncaught ReferenceError: foo is not defined
    at <anonymous>:1:1
Read more comments on GitHub >

github_iconTop Results From Across the Web

undefined - JavaScript - MDN Web Docs - Mozilla
A function returns undefined if a value was not returned . ... One reason to use typeof is that it does not throw...
Read more >
Documentation - TypeScript 3.7
only checks for whether the value on the left of it is null or undefined - not any of the subsequent properties. You...
Read more >
"undefined" should not be passed as the value of optional ...
TypeScript static code analysis. Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your TYPESCRIPT code.
Read more >
Why is undefined assignable to void? - Stack Overflow
In strict null checking mode, the null and undefined values are not in the domain of every type and are only assignable to...
Read more >
Cannot Read Property of Undefined in JavaScript - Rollbar
In JavaScript, properties and functions can only belong to objects. Since undefined is not an object type, calling a function or a property...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found