1 # Disallow use of the void operator. (no-void)
3 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:
5 The common case of using `void` operator is to get a "pure" `undefined` value as prior to ES5 the `undefined` variable was mutable:
8 // will always return undefined
13 // will return 1 in ES3 and undefined in ES5+
19 // will throw TypeError in ES5+
26 Another common case is to minify code as `void 0` is shorter than `undefined`:
33 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:
37 void function(){ foo = 1; }() // will assign foo a value of 1
38 +function(){ foo = 1; }() // same as above
42 function(){ foo = 1; }() // will throw SyntaxError
45 Some code styles prohibit `void` operator, marking it as non-obvious and hard to read.
49 This rule aims to eliminate use of void operator.
51 Examples of **incorrect** code for this rule:
54 /*eslint no-void: "error"*/
67 This rule has an object option:
69 * `allowAsStatement` set to `true` allows the void operator to be used as a statement (Default `false`).
73 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.
75 Examples of **incorrect** code for `{ "allowAsStatement": true }`:
78 /*eslint no-void: ["error", { "allowAsStatement": true }]*/
86 Examples of **correct** code for `{ "allowAsStatement": true }`:
89 /*eslint no-void: ["error", { "allowAsStatement": true }]*/
97 If you intentionally use the `void` operator then you can disable this rule.
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)
106 * [no-undef-init](no-undef-init.md)
107 * [no-undefined](no-undefined.md)