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.
I see people write void(0) all the time, but I don’t understand why people
Share
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)and0are the same thing, so this is how it would be evaluated:Which is identical to:
I guess the reason it’s being written as a function invocation is because people feel more comfortable with functions when used inline 🙂