Suggest an editImprove this articleRefine the answer for “The void operator”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`void` evaluates the expression to its right and always returns `undefined`, no matter what that expression produces.** The side effects still happen, only the value is discarded. ```javascript void 123; // undefined void (5 + 5); // undefined console.log(void calc()); // calc() runs, the result is undefined ``` **Key point:** `void` is for cases where you must run something and guarantee that nothing is returned: arrow functions without a return value, bookmarklets, and the historical `javascript:void(0)`.Shown above the full answer for quick recall.Answer (EN)Image**The `void` operator evaluates an expression and always returns `undefined`, whatever the expression itself produces.** It is a rare but useful tool when you need to run something and guarantee that nothing comes back. ## Theory ### TL;DR - Syntax: `void expression` or `void(expression)`. - The expression really is evaluated, so all of its side effects happen. - The result is always `undefined`, independently of the expression. - Classic scenarios: `javascript:void(0)` links, bookmarklets, IIFEs, arrow functions that must not return a value. - `void 0` is also a reliable way to obtain `undefined` where the name `undefined` itself could have been shadowed. ### Quick example ```javascript void 123; // undefined void 'hello'; // undefined void (5 + 5); // undefined ``` It does not matter what the expression on the right evaluates to: the result of `void` is always `undefined`. ### How void works The operator **runs the expression** but **always returns** `undefined`. The function is genuinely called: ```javascript function sayHi() { console.log('Hello!'); return 'Hi'; } console.log(sayHi()); // "Hi" console.log(void sayHi()); // runs, but returns undefined ``` Here `sayHi()` is called and prints to the console, but its result `'Hi'` is **ignored** and `undefined` comes out instead. There is also a historical reason for the operator: in old code `undefined` was not a reserved word and could be shadowed inside a scope. Writing `void 0` always produced the real `undefined`. ### Where it is used in practice #### 1. To make a link go nowhere Sometimes a click on a link should do nothing: ```html <a href="javascript:void(0)">Click me</a> ``` Here `void(0)` is evaluated, the result is `undefined`, and the browser does not navigate anywhere because `href` yields no address. This is an **outdated practice**: modern code should use `href="#"` together with `event.preventDefault()`, or just a `<button>`. #### 2. For IIFEs `void` used to be placed before a function to guarantee that an IIFE executed correctly: ```javascript void function() { console.log('It ran!'); }(); ``` Without `void` the parser could read the expression as a function declaration. Today this is rarely needed, but historically `void` served exactly as that safety net. #### 3. In arrow functions, to return nothing ```javascript const log = msg => void console.log(msg); log('Hello'); // prints to the console, but returns undefined ``` Here `void` is used to **perform an action without returning a value**. This is handy when an API expects a callback that must return `undefined` rather than some accidental value. #### 4. In bookmarklets, the mini scripts stored in bookmarks ```javascript javascript:void(alert('Hello')) ``` Without `void` the browser would try to navigate to a "page" holding the result of the expression, and `void` prevents that. ### Compared with a plain call ```javascript function calc() { return 42; } console.log(calc()); // 42 console.log(void calc()); // undefined ``` The function runs in both cases, but `void` forces the expression to return `undefined`. | Property | Value | | --- | --- | | What it does | Evaluates the expression and returns `undefined` | | What it returns | `undefined` | | What it is for | Ignoring the result of an expression | | Common scenarios | `javascript:void(0)` links, IIFEs, arrow functions without a return | ### Common mistakes - **Thinking `void` cancels execution.** The expression is evaluated in full, only its value is discarded. - **Using `javascript:void(0)` in new code.** For an action without navigation use a `<button>` or `event.preventDefault()`. - **Confusing `void` with `undefined` as a type.** It is an operator, not a type; the similarly named type belongs to TypeScript and means something else. - **Forgetting operator precedence.** `void a + b` parses as `(void a) + b`, so parentheses are mandatory in compound expressions. - **Applying `void` to an async function and assuming errors are gone.** The promise still runs, and an unhandled rejection does not disappear.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.