This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

void

Summary

The void operator evaluates an expression and then returns undefined.

Syntax

void expression

Examples

In this example, void(0) is used to return undefined to the link’s href property, so that the browser will not attempt to load a URL and the function in the onclick event can be executed instead.

<!DOCTYPE html>
<html>
<head>
<title>JavaScript void example</title>
</head>
<body>
<a href="javascript:void(0)" onclick="myFunction()">Click here to execute the function.</a>
</body>
</html>

Remarks

The expression argument is any valid JavaScript expression.

The void operator evaluates its expression and returns undefined. It is useful in situations where an expression should be evaluated but you do not want a result returned to the rest of the script.

Void works only on unary expressions, thus: void x, y; will set only the x variable to undefined.

Notes

void(0) is equivalent to void 0 and returns the undefined value.

Attributions

  • Microsoft Developer Network: Article