javascript:void(0) is a link target that does nothing: void 0 evaluates to undefined, and a browser that follows a javascript: URL only replaces the page when the result is something other than undefined. void 0 on its own is just another way to write undefined.
The two forms you will see in the wild are void 0 inside scripts and javascript:void(0) inside an href attribute. They rely on the same operator, so this article explains the operator first and then the link trick.
What void 0 means in JavaScript
void is a unary operator. Per MDN, it evaluates the expression after it and then returns undefined, whatever the expression produced. The syntax is:
void expression
Because the result is always undefined, the choice of expression only matters for its side effects. 0 has none, so void 0 is the shortest way to get the undefined value. MDN states that void(0) is equivalent to void 0; the parentheses are just grouping, not a function call. Here is a quick check you can paste into a browser console or Node.js:
console.log(void 0); // undefined
console.log(void 0 === undefined); // true
console.log(void "anything"); // undefined
void console.log("evaluated"); // logs "evaluated", result discarded
The last line shows the useful part of the operator. The expression runs, including any side effects, and only its return value is thrown away.
Watch the operator precedence
void binds tighter than comparison operators. MDN gives these two examples:
void 2 === "2"; // (void 2) === "2", so false
void (2 === "2"); // void (2 === "2"), so undefined
If you want to discard the result of a larger expression, wrap that expression in parentheses first.
Why people wrote void 0 instead of undefined
undefined is a global variable name, not a keyword, so code in a function or module scope can declare its own variable with that name. This is legal:
function demo() {
const undefined = 42;
return undefined;
}
console.log(demo()); // 42
Inside demo, the word undefined no longer means the undefined value. void 0 cannot be shadowed this way, because void is a reserved word, so older code and some minifiers use it as a guaranteed way to get the real value. MDN's advice is modest: where possible, use the global undefined. In modern code, nobody shadows undefined on purpose, and undefined is easier to read. If you compare against it, value === undefined is clearer than value === void 0. If you want to treat both null and undefined alike, see the == null case in == vs === in JavaScript.
What javascript:void(0) does in a link
A javascript: URL runs code instead of loading a page. MDN describes the rule: when a browser follows a javascript: URI, it replaces the page contents with the returned value, unless that value is undefined. That is why void appears here. It forces the result to undefined so the page stays put. MDN's examples:
<a href="javascript:void(0);">Click here to do nothing</a>
<a href="javascript:void(document.body.style.backgroundColor='green');">
Click here for green background
</a>
The second link runs an assignment, which would normally return the string "green". Without void, the browser would replace the whole page with the text green. With it, the colour changes and the page stays.
People used href="javascript:void(0)" mainly so a link would not jump to the top of the page the way href="#" does, while a separate onclick handler did the real work.
Why you should not use it any more
MDN says plainly that the javascript: pseudo protocol is discouraged in favor of alternatives such as unobtrusive event handlers. There are three practical reasons.
- It is treated like
eval. ESLint'sno-script-urlrule disallowsjavascript:URLs and explains that some consider them a form ofeval, because the browser has to parse and evaluate the code in the same way. - Content Security Policy. MDN's
script-srcpage says inline event handlers are blocked by default under a CSP, and the recommended fix isaddEventListener. Ajavascript:URL is inline code too, so a strict policy is likely to block it. Test it in your own policy rather than assuming. - Meaning. A link goes somewhere. A control that performs an action on the page is a button. Screen readers and keyboards treat the two differently, and a button gets keyboard activation for free.
What to use instead
For an action, use a button and attach the handler in script:
<button type="button" id="save">Save</button>
<script>
document.getElementById("save").addEventListener("click", () => {
// do the work here
});
</script>
If it really must be a link that sometimes does something other than navigate, give it a real destination in href and cancel the navigation in the handler with event.preventDefault(). Then the link still works when scripts fail to load.
If you only need a placeholder while building a page, href="#" is not harmful, but it scrolls to the top. A button avoids that without any trick.
Other places void shows up
Immediately invoked function expressions
A function declaration cannot be called directly, so the function keyword has to sit where an expression is expected. A unary operator does that, and MDN shows this form:
void function () {
console.log("Executed!");
}();
// Logs "Executed!"
MDN notes it is a bit longer than wrapping the function in parentheses, which has the same effect, but says void offers the best semantics because it signals that the return value is discarded. Without either wrapper, function () {...}() is a syntax error ("Function statements require a function name").
Arrow functions that must return nothing
This is the one use of void that is still common in current code. A braceless arrow function returns the value of its expression. MDN's example: if doSomething() returns false, this handler would cancel the checkbox's default action:
checkbox.onclick = () => doSomething();
Adding void makes sure the return value is ignored, even if the function changes later:
checkbox.onclick = () => void doSomething();
Note that void cannot be used to skip the parentheses when invoking an arrow function directly. void () => {...}() gives a syntax error.
Floating promises
Some linters flag a promise that is started and never awaited. Writing void somePromise() is a common way to mark that as deliberate. This is a convention of those tools rather than something the language requires, so check your own lint configuration before relying on it.
Quick reference
| You see | It means | Prefer today |
|---|---|---|
void 0 | The undefined value | undefined |
href="javascript:void(0)" | A link that does nothing | A <button type="button"> with addEventListener |
void function () {}() | An IIFE with the result discarded | Either form; a module or block scope often removes the need |
() => void fn() | Arrow function that always returns undefined | Keep it |
If a javascript:void(0) link does not work
If the link does nothing when you expect an action, the handler is probably missing or throwing. Open the browser console and click the link. An error there points at the handler. If the console reports a Content Security Policy violation, the policy is blocking inline code, and the fix is to move the logic into a script file and attach it with addEventListener. If clicking replaces the page with text such as undefined or [object Object], the void is missing and the expression returned a value.
Frequently asked questions
- What does javascript:void(0) do in a link?
- It runs the void operator on 0, which returns undefined. When a browser follows a javascript: URL, it replaces the page with the result unless that result is undefined, so javascript:void(0) makes the link do nothing.
- Is void 0 the same as undefined?
- Yes. void 0 evaluates 0 and returns the undefined value, and MDN says void(0) is equivalent to void 0. The global undefined can be used instead where possible, which usually reads better.
- Should I still use href="javascript:void(0)"?
- No. MDN says the javascript: pseudo protocol is discouraged in favor of alternatives such as unobtrusive event handlers. For something that performs an action, use a button element and attach the handler with addEventListener.
- Does void 0 work with a Content Security Policy?
- Not as a link target. MDN's script-src page says a CSP blocks inline event handlers by default, and a javascript: URL is inline code as well. Moving the logic into a script file and attaching it with addEventListener avoids the problem.