Say something about undefined
See original GitHub issueI 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:
- Created 11 years ago
- Reactions:2
- Comments:26 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The reason why comparision to
undefined
is not practiced, is that in most old browser engines you can overwrite it:Therefore it’s not safe. Imagine also small mistake in a code that inserts
<div id="undefined"></div>
to the document, after that all yourundefined
comparisons are doomed.This is the only way it can happen:
No
<div id="undefined">
, nowindow.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, whereundefined
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 tovoid 0
or something like that? Why does it have to be something a programmer needs to worry about?