javascript
  1. javascript-void

JavaScript Void

The void operator in JavaScript is used to evaluate an expression and return undefined as a result. Its syntax is as follows:

void expression;

Here, expression can be any JavaScript expression that needs to be evaluated. The void operator takes the result of this expression and returns undefined.

Example

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

The above code snippet is commonly used to create a dummy link that does not cause the page to refresh or navigate to a different page.

Output

The void operator always returns undefined, so there is no visible output.

Explanation

The void operator is useful in situations where a JavaScript expression needs to be evaluated, but the result is not needed. For example, when creating a dummy link that needs to be clickable but should not navigate to a new page.

Use

The void operator can be used to prevent the default behavior of certain HTML elements such as links and buttons. It can also be used to avoid value returning in a bookmarklet, where a script is executed by the user clicking on a bookmark that contains a JavaScript URI.

Important Points

  • The void operator can only take one argument, which is the expression that needs to be evaluated.
  • The void operator always returns undefined.
  • The void operator is often used to create dummy links or prevent navigation from certain HTML elements.

Summary

In JavaScript, the void operator is used to evaluate an expression and return undefined as the result. It can be used to create dummy links or prevent navigation from certain HTML elements. The void operator always returns undefined and can only take one argument, which is the expression to be evaluated.

Published on: