JavaScript Void 0: Exploring the Mysteries and Realities

The term “void” means “empty space” in the dictionary. In programming, when we use this term, it means returning “nothing” or an “empty value“.

What is the void keyword?

If a function is void, it means the function does not give back anything. It’s like functions in JavaScript that absolutely return undefined.

For example:

function person() {
  return undefined
}
person()

or like this example:

function person() {
}
person()

No matter what operations or instructions are included in the functions above (such as adding four numbers or finding the average of 8 numbers), they do not provide any output.

Now that we understand the purpose of the “void” keyword. But what does “javascript:void(0)” mean?

What does javascript:void(0) mean?

If we have divided this, we have javascript: and void(0).

Let’s explore at each part in more specifics.

  • javascript:
    • This is called a Pseudo URL. When a web browser get this value as the “href” value of a link, it reads the JavaScript code after the colon (:)or rather handling the value as a normal website address.

Here’s an example code:

<a href="javascript:console.log('javascriptSample');alert('javascript Programm Succesfully Sent a Message')">Example Link</a>

When you run the code above and click the Example link the output will be:

What does javascript:void(0) mean?

As you can see in the above example, the browser doesn’t handle href as a regular path. Rather, it manage some JavaScript code and begin after “javascript:” and separated by semi-colons.

  • void(0)
    • The void operator will check the expressions and gives back an “undefined” result.

Let’s take a look at the example code here:

const adition = void(3 + 3);
console.log(adition);

When we add 3 + 3 together, we get a result, but it is not defined or clear.

Let me show you another example to confirm this:

<body>
  <h1>Welcome to JavaScript Void Tutorial</h1>
  <script>
        void(document.body.style.backgroundColor = 'blue',
             document.body.style.color = 'white'
        )
  </script>
</body>

Output:

JavaScript Void 0 example

How to combine “javascript:” and “void(0)” together?

Sometimes, you may not want a link to take you to a another page or refresh the page. By using JavaScript:, you can execute the code that doesn’t change the current page.

If joined with void(0), it is essentially means doing nothing – no refreshing, no navigation, and no execution of any code.

Here’s an example:

<a href="javascript:void(0)">Click the Link Here</a>

The word “Link” is regarded as a clickable word in a web browser. It can be preferred, but it does not take you to a different page.

When we pass 0 as an argument to the void function, it does not do anything and isn’t give any result.

You can also pass JavaScript code (like the one shown above) as an argument to the void function.

This enables the link to execute some code while staying on the same page.

Here’s an example code:

<a id="link" href="javascript:void(
document.querySelector('#link').style.color = 'red'
);">Click the Link Here!</a>

Output:

How to combine "javascript:" and "void(0)" together?

When using “void“, it will notify the browser to not show anything or give any result.

Another reason to use “javascript:void(0)” for links is when we want to run some JavaScript code without actually managing to a new page.

In such cases, we use the expressions as arguments with void.

Conclusion

In this article, we learned about the void operator. It helps us avoid a webpage from going to a different page or reloading when we click on a link with the javascript: pseudo URL.

Additional Resources

Leave a Comment