JavaScript programming language provides the void
keyword which is popularly used as javascipt:void(0)
. The void is a special operator which evaluates provided expression as a parameter and returns null. In every case, the void() returns null. As the void dictionary, meaning is completely empty space or nothing.
javascript:void(0) Returns as undefined
The javascript:void(0)
generally used with HTML links to set the href attribute as null in an elegant way. The onlick()
or similar event is defined but the href is set as undefined
with the “javascript:void(0)”. In the following HTML and JavaScript code, even the user clicks on the link it does not open any new page and just executes the click event.
<a href="javascript:void(0)" onclick="display()">Display</a>
Evaluate Different Values with void()
Actually, the void() method returns undefined even some data is provided as a parameter. In the following examples, every void() execution returns undefined.
void(1+2)
//undefined
void("https://wwww.wisetut.com")
//undefined
“javascript:void(0)” Alternative “href=#”
As the “javascript:void(0)” is used to specify an undefined or empty link with the href attribute there is an alternative. The “href=#” can be used to specify empty links which may be a better implementation because it does not require any JavaScript.
<a href=# onclick="display()">Display</a>