The void operator

suggest change

The void operator evaluates the given expression and then returns undefined.

Syntax:

void expression

Returns:

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');

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents