Skip to content

Commit

Permalink
Docs: list another related rule in no-undefined (#8467)
Browse files Browse the repository at this point in the history
* Docs: add related rules, context to no-undefined

* Docs: tweak phrasing of background info for no-undefined rule
  • Loading branch information
ethanresnick authored and not-an-aardvark committed Apr 23, 2017
1 parent f987814 commit 9c3da77
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions docs/rules/no-undefined.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Disallow Use of `undefined` Variable (no-undefined)

The `undefined` variable is unique in JavaScript because it is actually a property of the global object. As such, in ECMAScript 3 it was possible to overwrite the value of `undefined`. While ECMAScript 5 disallows overwriting `undefined`, it's still possible to shadow `undefined`, such as:
The `undefined` variable in JavaScript is actually a property of the global object. As such, in ECMAScript 3 it was possible to overwrite the value of `undefined`. While ECMAScript 5 disallows overwriting `undefined`, it's still possible to shadow `undefined`, such as:

```js
function doSomething(data) {
Expand All @@ -14,24 +14,15 @@ function doSomething(data) {
}
```

This represents a problem for `undefined` that doesn't exist for `null`, which is a keyword and primitive value that can neither be overwritten nor shadowed.
Because `undefined` can be overwritten or shadowed, reading `undefined` can give an unexpected value. (This is not the case for `null`, which is a keyword that always produces the same value.) To guard against this, you can avoid all uses of `undefined`, which is what some style guides recommend and what this rule enforces. Those style guides then also recommend:

All uninitialized variables automatically get the value of `undefined`:

```js
var foo;

console.log(foo === undefined); // true (assuming no shadowing)
```

For this reason, it's not necessary to explicitly initialize a variable to `undefined`.

Taking all of this into account, some style guides forbid the use of `undefined`, recommending instead:

* Variables that should be `undefined` are simply left uninitialized.
* Variables that should be `undefined` are simply left uninitialized. (All uninitialized variables automatically get the value of `undefined` in JavaScript.)
* Checking if a value is `undefined` should be done with `typeof`.
* Using the `void` operator to generate the value of `undefined` if necessary.

As an alternative, you can use the [no-global-assign](no-global-assign.md) and [no-shadow-restricted-names](no-shadow-restricted-names.md) rules to prevent `undefined` from being shadowed or assigned a different value. This ensures that `undefined` will always hold its original, expected value.


## Rule Details

This rule aims to eliminate the use of `undefined`, and as such, generates a warning whenever it is used.
Expand Down Expand Up @@ -84,3 +75,5 @@ If you want to allow the use of `undefined` in your code, then you can safely tu

* [no-undef-init](no-undef-init.md)
* [no-void](no-void.md)
* [no-shadow-restricted-names](no-shadow-restricted-names.md)
* [no-global-assign](no-global-assign.md)

0 comments on commit 9c3da77

Please sign in to comment.