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.

However, in older versions of ECMAScript, window.undefined could be assigned any value, and it is still possible to use undefined as name for function parameters variables inside functions, thus disrupting other code that relies on the value of undefined.

void will always yield the true undefined value though.

void 0 is also commonly used in code minification as a shorter way of writing undefined. In addition, it’s probably safer as some other code could’ve tampered with window.undefined.

Examples:

Returning undefined:

function foo(){
    return void 0;
}
console.log(foo()); // undefined

Changing the value of undefined inside a certain scope:

(function(undefined){
    var str = 'foo';
    console.log(str === undefined); // true
})('foo');

Feedback about page:

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


Unary Operators:
* Syntax
* The void operator

Table Of Contents
11 Arrays
12 Objects
14 Classes
16 Map
17 Set
21 Unary Operators
24 Loops
27 Date
29 Scope
30 AJAX
35 Cookies
41 JSON
44 Fetch
45 Modules
46 Screen
64 Console
68 Symbols
73 Modals
76 Events
86 Proxy
89 WeakMap
90 WeakSet
102 Tilde