The void operator
suggest changeThe void operator evaluates the given expression and then returns undefined.
Syntax:
void expression
Returns:
undefined
Description
The void operator is often used to obtain the undefined primitive value, by means of writing void 0 or void(0). Note that void is an operator, not a function, so () is not required.
Usually the result of a void expression and undefined can be used interchangeably.
Before ES5, window.undefined could be assigned another value. The global binding is read-only now, but a function parameter or local variable can still be named undefined and hide the real value.
void will always yield the true undefined value though.
void 0 is also a short way to write undefined in minified code, and it is unaffected by a local binding named undefined.
Examples:
Returning undefined:
function foo(){
return void 0;
}
console.log(foo()); // undefined
Changing the value of undefined inside a certain scope:
(function(undefined){
const str = 'foo';
console.log(str === undefined); // true
})('foo');