Many people write `javascript:void(0)` instead of `javascript:void 0` in hrefs. Do the parentheses do anything?

javascript

Solution

I have to admit that I've used that same construct many times in the past, mainly because I've seen it being used on other sites. I'm no longer using this because unobtrusive JavaScript is preferred over inline JavaScript; in fact, it's almost exclusively used inline to make sure the page doesn't refresh.

Having said that, as you have rightfully pointed out, it's an operator and not a function; the reason it still works is simply because `(0)` and `0` are the same thing, so this is how it would be evaluated:

void (0);

Which is identical to:

void 0;

I guess the reason it's being written as a function invocation is because people feel more comfortable with functions when used inline :)

<a href="javascript:void 0">...</a> // hold on, that can't work, can it?!

<a href="javascript:void(0)">...</a> // ahhh, normality restored

Problem

I see people write `void(0)` all the time, but I don't understand why people use parentheses. As far as I can tell, they have no purpose. `void` is not a function, it's an operator. So why do people use the parens? Do they serve a purpose? Even on MDN the parens are used.

Original source