]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-void.md
import 8.4.0 source
[pve-eslint.git] / eslint / docs / rules / no-void.md
CommitLineData
eb39fafa
DC
1# Disallow use of the void operator. (no-void)
2
3The `void` operator takes an operand and returns `undefined`: `void expression` will evaluate `expression` and return `undefined`. It can be used to ignore any side effects `expression` may produce:
4
5The common case of using `void` operator is to get a "pure" `undefined` value as prior to ES5 the `undefined` variable was mutable:
6
7```js
8// will always return undefined
9(function(){
10 return void 0;
11})();
12
13// will return 1 in ES3 and undefined in ES5+
14(function(){
15 undefined = 1;
16 return undefined;
17})();
18
19// will throw TypeError in ES5+
20(function(){
21 'use strict';
22 undefined = 1;
23})();
24```
25
26Another common case is to minify code as `void 0` is shorter than `undefined`:
27
28```js
29foo = void 0;
30foo = undefined;
31```
32
33When used with IIFE (immediately-invoked function expression), `void` can be used to force the function keyword to be treated as an expression instead of a declaration:
34
35```js
36var foo = 1;
37void function(){ foo = 1; }() // will assign foo a value of 1
38+function(){ foo = 1; }() // same as above
39```
40
34eeec05 41```js
eb39fafa
DC
42function(){ foo = 1; }() // will throw SyntaxError
43```
44
45Some code styles prohibit `void` operator, marking it as non-obvious and hard to read.
46
47## Rule Details
48
49This rule aims to eliminate use of void operator.
50
51Examples of **incorrect** code for this rule:
52
53```js
54/*eslint no-void: "error"*/
55
56void foo
57void someFunction();
58
59var foo = void bar();
60function baz() {
61 return void 0;
62}
63```
64
65## Options
66
67This rule has an object option:
68
69* `allowAsStatement` set to `true` allows the void operator to be used as a statement (Default `false`).
70
71### allowAsStatement
72
73When `allowAsStatement` is set to true, the rule will not error on cases that the void operator is used as a statement, i.e. when it's not used in an expression position, like in a variable assignment or a function return.
74
75Examples of **incorrect** code for `{ "allowAsStatement": true }`:
76
77```js
78/*eslint no-void: ["error", { "allowAsStatement": true }]*/
79
80var foo = void bar();
81function baz() {
82 return void 0;
83}
84```
85
86Examples of **correct** code for `{ "allowAsStatement": true }`:
87
88```js
89/*eslint no-void: ["error", { "allowAsStatement": true }]*/
90
91void foo;
92void someFunction();
93```
94
95## When Not To Use It
96
97If you intentionally use the `void` operator then you can disable this rule.
98
99## Further Reading
100
101* [Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void)
102* [Bad Parts: Appendix B - JavaScript: The Good Parts by Douglas Crockford](https://oreilly.com/javascript/excerpts/javascript-good-parts/bad-parts.html)
103
104## Related Rules
105
106* [no-undef-init](no-undef-init.md)
107* [no-undefined](no-undefined.md)