]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-void.md
5f9d24db176711bd0683b3bbcca24fdde81bfd02
[pve-eslint.git] / eslint / docs / src / rules / no-void.md
1 ---
2 title: no-void
3 layout: doc
4 rule_type: suggestion
5 related_rules:
6 - no-undef-init
7 - no-undefined
8 further_reading:
9 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void
10 - https://oreilly.com/javascript/excerpts/javascript-good-parts/bad-parts.html
11 ---
12
13
14 The `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:
15
16 The common case of using `void` operator is to get a "pure" `undefined` value as prior to ES5 the `undefined` variable was mutable:
17
18 ```js
19 // will always return undefined
20 (function(){
21 return void 0;
22 })();
23
24 // will return 1 in ES3 and undefined in ES5+
25 (function(){
26 undefined = 1;
27 return undefined;
28 })();
29
30 // will throw TypeError in ES5+
31 (function(){
32 'use strict';
33 undefined = 1;
34 })();
35 ```
36
37 Another common case is to minify code as `void 0` is shorter than `undefined`:
38
39 ```js
40 foo = void 0;
41 foo = undefined;
42 ```
43
44 When 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:
45
46 ```js
47 var foo = 1;
48 void function(){ foo = 1; }() // will assign foo a value of 1
49 +function(){ foo = 1; }() // same as above
50 ```
51
52 ```js
53 function(){ foo = 1; }() // will throw SyntaxError
54 ```
55
56 Some code styles prohibit `void` operator, marking it as non-obvious and hard to read.
57
58 ## Rule Details
59
60 This rule aims to eliminate use of void operator.
61
62 Examples of **incorrect** code for this rule:
63
64 ::: incorrect
65
66 ```js
67 /*eslint no-void: "error"*/
68
69 void foo
70 void someFunction();
71
72 var foo = void bar();
73 function baz() {
74 return void 0;
75 }
76 ```
77
78 :::
79
80 ## Options
81
82 This rule has an object option:
83
84 * `allowAsStatement` set to `true` allows the void operator to be used as a statement (Default `false`).
85
86 ### allowAsStatement
87
88 When `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.
89
90 Examples of **incorrect** code for `{ "allowAsStatement": true }`:
91
92 ::: incorrect
93
94 ```js
95 /*eslint no-void: ["error", { "allowAsStatement": true }]*/
96
97 var foo = void bar();
98 function baz() {
99 return void 0;
100 }
101 ```
102
103 :::
104
105 Examples of **correct** code for `{ "allowAsStatement": true }`:
106
107 ::: correct
108
109 ```js
110 /*eslint no-void: ["error", { "allowAsStatement": true }]*/
111
112 void foo;
113 void someFunction();
114 ```
115
116 :::
117
118 ## When Not To Use It
119
120 If you intentionally use the `void` operator then you can disable this rule.