JavaScript Void


javascript:void(0) Meaning

We often use codes such as javascript:void(0) , so what does javascript:void(0) mean in JavaScript ?

The most critical keyword in javascript:void(0) is the void keyword. Void is a very important keyword in JavaScript. This operator specifies that an expression is to be calculated but no value is returned.

The syntax format is as follows:

void func()
javascript:void func()

or

void(func())
javascript:void(func())

The following code creates a hyperlink, and nothing happens when the user clicks it.

Example

<a href="javascript:void(0)">Click here and nothing will happen</a>

Try It! 

When the user links, void(0) is calculated as 0, but there is no effect on Javascript.

In the following example, a warning message is displayed after the user clicks on the link:

Example

<p>Click the link below to view the results:</p>
<a href="javascript:void(alert('Warning!!!'))">Click me!</a>

Try It! 

The parameter a in the following example will return undefined:

Example

function getValue(){
    var a,b,c;
    a = void (b = 5, c = 7 );
    document.write('a = '+ a +' b = '+ b +'c =' + c );
}

Try It! 


The difference between href="#" and href="javascript:void(0)"

# Contains a location information, the default anchor is #top, which is the top of the page.

And javascript:void(0), just means a dead link.

When the page is very long, # will be used to locate the specific location of the page, the format is: # + id .

If you want to define a dead link, please use javascript:void(0).

Example

<a href="javascript:void(0);">Click on what I did not respond!</a>
<a href="#pos">Click me to locate the specified location!</a>
<br>
...
<br>
<p id="pos">Tail anchor point</p>

Try It!