Disallow use of the void operator. (no-void)
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:
The common case of using void
operator is to get a “pure” undefined
value as prior to ES5 the undefined
variable was mutable:
// will always return undefined
(function(){
return void 0;
})();
// will return 1 in ES3 and undefined in ES5+
(function(){
undefined = 1;
return undefined;
})();
// will throw TypeError is ES5+
(function(){
'use strict';
undefined = 1;
})();
Another common case is to minify code as void 0
is shorter than undefined
:
foo = void 0;
foo = undefined;
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:
var foo = 1;
void function(){ foo = 1; }() // will assign foo a value of 1
+function(){ foo = 1; }() // same as above
function(){ foo = 1; }() // will throw SyntaxError
Some code styles prohibit void
operator marking it as non-obvious and hard to read.
Rule Details
This rule aims to eliminate use of void operator.
The following patterns are considered problems:
/*eslint no-void: 2*/
void foo /*error Expected 'undefined' and instead saw 'void'.*/
var foo = void bar(); /*error Expected 'undefined' and instead saw 'void'.*/
When Not To Use It
If you intentionally use the void
operator then you can disable this rule.
Further Reading
Related Rules
Version
This rule was introduced in ESLint 0.8.0.