What is javascript void(0)

javascript void(0) is an operator that executes an expression without reloading the web page and removes unwanted effects from the web page. It always returns an undefined primitive value.

The void(0) is mainly used with a hyperlink to prevent the web page from refreshing. This means that we can use it as javascript: void(0) in href does not have any URL but It will be performed on executing another expression.

syntax – 1

<a href="javascript:void(0)">Link Text</a>

Sntax – 2

<a href="javascript:void(expression)">Link Text</a>

The void(0) can accept an expression and return its result as well as an undefined value.

Example –

document.write(void());           // It returns syntexError
document.write(void(0));          // It returns undefined 
document.write(void(10));         // It returns undefined
document.write(void('coding'));   // It returns undefined
document.write(void(document.write(10)));   // It returns 10 as well as undefined
document.write(void(document.write('coding')));   // It returns 'coding' as well as undefined

What is the use of javascript void(0)?

Javascript void(0) is used to execute an expression on clicking a hyperlink. Even It stops the default refreshing properties of an anchor tag.

Read Also –

JavaScript Interview Questions and Answers

Change URL without Reloading Page Using jquery Ajax

Use javascript void(0) in href

You can use javascript void(0) in href attribute to perform different expressions. So, let’s understand its usage through the following examples –

Example – 1

This example will refresh the web page by clicking a hyperlink

<a href="#">Click me</a>

Example – 2

This example will not refresh the web page on clicking a hyperlink

<a href="javascript:void(0)">Click me</a>

Example – 3

This example will display a numeric value of 10 on clicking a hyperlink

<a href="javascript:void(document.write(10))">Click me</a>

Example – 4

This example will display a string value coding on clicking a hyperlink

<a href="javascript:void(document.write('coding'))">Click me</a>

Example – 5

This example will display an alert message on clicking a hyperlink

<a href="javascript:void(alert('Have you got it?'))">Click me</a>

Example – 6

This example will create a green background of the web page on clicking a hyperlink

<a href="javascript:void(document.body.style.backgroundColor='green')">click me</a>

Use javascript void(0) with onclick

You can use javascript void(0) with onclick attribute to execute an expression by clicking an anchor tag or another HTML elements such as <p>, <button>, <h2>, <b> & more.

Syntax –

<tagname onclick="javascript:void(expression)">Some Text</tagname>

Now, Let’s understand it through the following examples –

Example – 1

This example will open a new window on clicking the hyperlink

<a href="javascript:void(0)" onclick="window.open('https://codingstatus.com/', '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400')">Open New Window</a>

Example – 2

This example will show a numeric value 10 on clicking a paragraph element

<p onclick="javascript:void(document.write(10))">This is a paragraph text</p>

Example – 3

This example will show a string value Hello World on clicking a button

<button onclick="javascript:void(document.write('Hello World'))">This is a paragraph text</button>

Example – 4

This example will execute an internal javascript on clicking an anchor tag.

<a href="javascript:void(0)" onclick="showMessage()">Execute Now</a>

<script type="text/javascript">
  var showMessage= function(){
     alert('Hello Developer');
  }
</script>