JavaScript

How does void operator work in JavaScript

In JavaScript, an expression that is evaluated using the void operator, will always return undefined. If we look at the dictionary definition of the term void, we will come to know that the word void means “completely empty”. However, when it comes to the programming world, void means nothing will be returned. This means the void operator will be used with the methods that have nothing to return.

  • What is Void operator in JavaScript
  • Basic Syntax
  • What does javascript:void(0) means in JavaScript?
  • How to use void operator in JavaScript
  • Examples

So, let’s begin!

What is Void operator

It is a unary operator that is used to get undefined primitive values. In simple words, we can say that the void operator evaluates an expression and doesn’t return any value. The void operator is frequently used in conjunction with Hyperlinks

Basic Syntax

The below-given code block will show the basic syntax of the void operator:

void expression

What does javascript:void(0) means in JavaScript?

The “javascript:” is described as the Pseudo URL while the void operator evaluates an expression and doesn’t return any value. Hyperlinks are the most common use of javascript:void(0). Whenever a user clicks on a link on a webpage then a new page loads in most of the cases. But sometimes, we don’t want a URL to navigate to some other page or refresh a page. In such a case, the void(0) can be used to restrict a website from refreshing/reloading when a link is clicked.

How to use void operator in JavaScript

Let’s consider some examples to understand the working of void operator in JavaScript:

Example1

In this example, we will create two links, in the first link we will utilize the void(0) method while in the second link we will utilize the alert method:

<body>
  <h3>Javascript Void Method</h3>
  <a href="javascript:void(0)">CLICK ME</a>
  <h3>Javascript alert Method</h3>
  <a href="javascript:void(alert('Welcome to Linuxhint'))">CLICK ME</a>
</body>

The above snippet will generate the following output:

The output verified that when we clicked on link1, the void(0) method prevented it from refreshing.

Example2
In this example we will explain how to generate the undefined value using the void operator:

<html>
<head>
  <script type="text/javascript">
    function exampleFunction() {
      var num1, num2, num3, num4;

      num1 = 15, num2 = void (num3 = 37, num4 = 50);
      document.write('num1 = ' + num1 + ' num2 = ' + num2 + ' num3 = ' + num3 + ' num4 = ' + num4);
    }
  </script>
</head>

<body>
  <h3>Javascript Void Method</h3>
  <form>
    <input type="button" value="CLICK ME" onclick="exampleFunction();" />
  </form>
</body>
</html>

In this example, we created a method that will be invoked when someone clicks on the “CLICK ME” button. Within the method we created four variables and assigned some numeric values to all the variables except the second variable. We assigned void to the second variable.

From the output it is clear that the void operator assigned an undefined value to the second variable.

Conclusion

The void operator is a unary operator that is used to get the undefined primitive values. It evaluates an expression and doesn’t return any value and is commonly used in conjunction with Hyperlinks. This write-up explained various aspects of void operator with the help of some relevant examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.