According to the dictionary, the word void means “completely empty space”. This term, when used in programming, refers to the return of “nothing”, so to speak, an “empty value”.
What is the keyword void?
If the function is invalid, it means that the function returns nothing. The use of void operator is for the returns the undefined and evaluation of an expression. It is often used with hyperlinks. The [removed]void(0) can be used when we don`t want to refresh or load a new page in the browser by clicking a hyperlink.
Operand 0 can be used in two ways: void (0) or void0. Both methods work the same. The [deleted] void (0) tells the browser to “do nothing”. It avoids the browser from refreshing the page or reloading. It is useful to include links that play an important role in the web page without reloading. Therefore, using void (0) for such a link will prevent the page from reloading, but you can perform useful functions such as updating the value of the web page. This is similar to a JavaScript function that explicitly returns undefined as follows:
function and () {
returns undefined
}
When()
Or implicitly:
functions and () {
}
When()
No result is returned regardless of the expression and statement of the above function (for example, adding two numbers and averaging five numbers).
Now you know what the void keyword is. What about the [deleted] void (0)?
What is the [deleted] void (0)?
If you split it, you get Delete and void (0). We need to take a nearest peek at every piece.
[DATA EXPUNGED]
This is known as a pseudo URL. When the browser receives this value as the href value of the anchor tag, it interprets the JS code following the colon (:) instead of treating the value as a reference path.
Example:
link
When you click the “link”, the result looks like this:
As mentioned above, the browser does not treat href as a reference path. Instead, it starts with “[DATA EXPUNGED]” and is treated like a semicolon-separated JavaScript code.
Void (0)
The void operator evaluates the specified expression and returns undefined.
For example:
const result = void(1 + 1);
console.log(result);
// undefined
1 + 1 is evaluated but undefined is returned. To confirm that, here’s another example:
<body>
Heading
[removed]
void(document.body.style.backgroundColor = ‘red’,
document.body.style.color = ‘white’
)
[removed]
</body>
The result of the above code is:
Here’s another example:
console.log(void(0) === undefined)
// true
Combining [removed] and void(0)
Sometimes, you do not want a link to navigate to another page or reload a page. Using [removed], you can run code that does not change the current page.
This, utilized with void(0) means, don’t navigate, do anything don’t reload, do not run any code.
For example:
Link
The “Link” word is treated as a link by the browser. For example, you can focus, but you don’t go to a new page.
0 is an argument passed to void, does nothing and returns nothing.
JavaScript code (as above) can also be passed as an argument to the void method. This causes the link element to execute some code but keep the same page.
Example:
link
When you click the button, it looks like this: Specifying
void tells the browser to return nothing (or undefined). Another use case for a link whose reference is Delete void (0) is that the link may execute JavaScript code in the background and may not require navigation. In this case, the expression is used as an argument passed to void.
Conclusion
In this simplified article, you learned what the void operator is, how it works, and how to use it in the pseudo URL [DATA EXPUNGED] of the link’s href attribute.
This will prevent you from navigating to another page or reloading the current page when a page is clicked.