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.

Say something about undefined

See original GitHub issue

I find people treat undefined incorrectly a lot. It’s a value, you can use it, test for it, etc, but people frequently use typeof x == "undefined" when they don’t need to.

I made a fork with a big changeset of my own opinions, but here’s the section I wrote about undefined:


## <a name='undefined'>undefined</a>

  - `undefined` is an object.  You should use it as an object.  You can test for it.  For instance:

    ```javascript
    // good
    function pow(a, b) {
      if (b === undefined) {
        b = 1;
      }
      return Math.pow(a, b);
    }

    // bad
    function pow(a, b) {
      if (typeof b == "undefined") {
        b = 1;
      }
      return Math.pow(a, b);
    }
    ```

  - *Only* use `typeof x == "undefined"` when the variable (`x`) may not be declared, and it would be an error to test `x === undefined`:

    ```javascript
    if (typeof Module == "undefined") {
      Module = {};
    }

    // But also okay, for browser-only code:
    if (window.Module === undefined) {
      Module = {};
    }
    ```

    Note that you can't use `window` in Node.js; if you think your code could be used in a server context you should use the first form.

Issue Analytics

  • State:open
  • Created 11 years ago
  • Reactions:2
  • Comments:26 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
medikoocommented, Jun 5, 2018

The reason why comparision to undefined is not practiced, is that in most old browser engines you can overwrite it:

window.undefined = 'foo';
alert('foo' === undefined); // true

Therefore it’s not safe. Imagine also small mistake in a code that inserts <div id="undefined"></div> to the document, after that all your undefined comparisons are doomed.

0reactions
alecmevcommented, Jun 6, 2018

This is the only way it can happen:

const ourInnocentCode = 'console.log(undefined)';

{ // Or a closure
  const undefined = 'ha, not undefined'; // Or `let` / `var`
  eval(ourInnocentCode);
  // > "ha, not undefined"
}

No <div id="undefined">, no window.undefined = 'foo'. Can’t be affected by other <script>s. Can’t be naive concatenation, because it would put our code on the global scope, where undefined is safe. Is this really worth being protected against?

And even if it were (it isn’t, unless I’m missing something), why not solve it by wrapping the whole bundle in a closure/block, with a safe undefined, set to void 0 or something like that? Why does it have to be something a programmer needs to worry about?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Undefined - Definition, Meaning & Synonyms - Vocabulary.com
When you define something, that's the end of it — no more wondering. So if something is undefined, it's not yet determined. Definitions...
Read more >
Undefined Definition & Meaning - Dictionary.com
Undefined definition, without fixed limits; indefinite in form, extent, or application: undefined authority; undefined feelings of sadness. See more.
Read more >
Undefined Definition & Meaning - Merriam-Webster
The meaning of UNDEFINED is not defined. How to use undefined in a sentence.
Read more >
undefined - JavaScript - MDN Web Docs - Mozilla
undefined is a property of the global object. That is, it is a variable in global scope. In all non-legacy browsers, undefined is...
Read more >
Proof that something is undefined? - Math Stack Exchange
@user599310: Not giving something a name is not the same thing as not defining it. Something may be defined but not have a...
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