The void "Function" in JavaScript

Jul 22, 2021

JavaScript's void operator always returns undefined, no matter what argument you pass it. void 0 and void(0) are equivalent:

void 0; // undefined
void(0); // undefined

void(0) is often used as a href attribute on an a tag. The below syntax makes a do nothing, preventing an unwanted page refresh:

<a href="javascript:void(0)">
  Nothing Special Link
</a>

Although void(0) looks like a function call, remember that void is not a function.

void(void); // SyntaxError: Unexpected token ')'

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Fundamentals Tutorials