What Exactly Is “void 0” – And What’s It For?
Hey there, folks! Today, we’re going to talk all about one very specific command in JavaScript: “void (0)”, also seen as “void 0”. It’s a quirky command; one whose purpose many aren’t entirely clear on. To confuse things even more, there’s also an error message: “javascript:void(0)” that is basically unrelated. We’ll talk about both to clear up the confusion. Let’s see if we can clear things up a bit. Shall we begin?
The “javascript:void(0)” Error Message
Right. According to Tech-Faq, this is a fairly unique error, primarily because it can be encountered by users of pretty much any operating system.
There are quite a few issues which might cause this troublesome little error prompt to pop up:
- Conflicts with ad blockers/popup blockers
- Poorly-written JavaScript
- Disabled JavaScript
- An Outdated Web Browser
- Use of a proxy server
As I’m certain you’ve noticed, the error is caused by the user in all but the second case, where it’s usually caused by “web page authors who don’t check their script for compatibility.” In that circumstance, the user has no choice but to use the browser that the script was written for.
If you’re a developer or sysadmin, this is a reminder to test your scripts in as many browsers as possible.
And now for something completely different…with a very similar name.
The “void” Operator and “void 0”
“void” is an operator that can be applied to any argument in a script. The syntax goes a little something like this:
<head>
<script type=”text/javascript”>
<!–
void func()
javascript:void func()
or
void(func())
javascript:void(func())
//–>
</script>
</head>
When “void(0)” is applied to an expression or argument, the value returned is always “undefined”, regardless of the other values present. That may seem somewhat useless at first glance; and more than a little obtuse. After all, why not simply use “undefined” itself?
For that matter, why do you need any of this stuff at all?
A couple reasons. Let’s start by explaining why the “undefined” value is important in the first place. Believe it or not, there’s actually a ton of different reasons one might use it.
Why Use “void”?
According to Tizag, one of the most common uses for “void” is so that JavaScript can be executed in a web browser without having to load a new page. See, most browsers allow you to run scripts by entering it into the URL bar. With “void (0)”, you can do this without having to switch pages – it effectively cancels out the page load.
This allows you to build a link whose sole purpose is the execution of JavaScript code (though some might argue that isn’t really the purpose of a link).
“Web browsers will try to take whatever is used as a URL and load it,” reads Tizag’s tutorial. “The only reason we can use a JavaScript Alert statement without loading a new page is because alert is a function that returns a null value. That means that when the browser attempts to load a new page it sees null and has nothing to load.”
“The important thing to notice here,” the tutorial continues, “is that if you ever do use a JavaScript statement as the URL that returns a value, the browser will attempt to load a page. To prevent this unwanted action, you need to use the void function on such statement, which will always return null and never load a new page.”
What’s The Difference Between “void (0)” and “undefined”?
The problem with simply using “undefined” instead of “void (0)” is that the former isn’t a reserved word – it’s generally a variable, rather than an operator; in some JavaScript environments, it’s actually a global. It may occasionally have the value of “undefined”, but that’s not guaranteed, because somebody can always assign it a value that isn’t “undefined.”
On the other hand, “void” always returns the value of “undefined”. As far as the difference between “void (0)” and…well “void” with any other characters; that’s kind of irrelevant. “void “dingleberries”” would work just as well as “void (0)” or “void 0”.
Image credit: Andreas