Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

void operator

BaselineWidely available

Thevoid operator evaluates the givenexpression and then returnsundefined.

Try it

const output = void 1;console.log(output);// Expected output: undefinedvoid console.log("expression evaluated");// Expected output: "expression evaluated"void (function iife() {  console.log("iife is executed");})();// Expected output: "iife is executed"void function test() {  console.log("test function executed");};try {  test();} catch (e) {  console.log("test function is not defined");  // Expected output: "test function is not defined"}

Syntax

js
void expression

Description

This operator allows evaluating expressions that produce a value into places where anexpression that evaluates toundefined is desired.

Thevoid operator is often used merely to obtain theundefined primitive value, usually usingvoid(0) (which isequivalent tovoid 0). In these cases, the global variableundefined can be used.

It should be noted thatthe precedenceof thevoid operator should be taken into account and thatparentheses can help clarify the resolution of the expression following thevoid operator:

js
void 2 === "2"; // (void 2) === '2', returns falsevoid (2 === "2"); // void (2 === '2'), returns undefined

Examples

Immediately Invoked Function Expressions

When using animmediately-invoked function expression, thefunction keyword cannot be at the immediate start of thestatement, because that would be parsed as afunction declaration, and would generate a syntax error when the parentheses representing invocation is reached — if the function is unnamed, it would immediately be a syntax error if the function is parsed as a declaration.

js
function iife() {  console.log("Executed!");}(); // SyntaxError: Unexpected token ')'function () {  console.log("Executed!");}(); // SyntaxError: Function statements require a function name

In order for the function to be parsed as anexpression, thefunction keyword has to appear at a position that only accepts expressions, not statements. This can be achieved by prefixing the keyword with aunary operator, which only accepts expressions as operands. Function invocation has higherprecedence than unary operators, so it will be executed first. Its return value (which is almost alwaysundefined) will be passed to the unary operator and then immediately discarded.

Of all the unary operators,void offers the best semantic, because it clearly signals that the return value of the function invocation should be discarded.

js
void function () {  console.log("Executed!");}();// Logs "Executed!"

This is a bit longer than wrapping the function expression in parentheses, which has the same effect of forcing thefunction keyword to be parsed as the start of an expression instead of a statement.

js
(function () {  console.log("Executed!");})();

Note that this trick only applies to IIFEs defined with thefunction keyword. Attempting to use thevoid operator to avoid parentheses for an arrow function results in a syntax error. Arrow function expressions always require parentheses around them when being called.

js
void () => { console.log("iife!"); }(); // SyntaxError: Malformed arrow function parameter list

JavaScript URIs

When a browser follows a#"/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined">undefined. Thevoid operator can be used to returnundefined. For example:

html
<a href="#">Click here to do nothing</a><a href="#">  Click here for green background</a>

Note:#"non-leaking_arrow_functions">

Non-leaking Arrow Functions

Arrow functions introduce a short-hand braceless syntax that returns an expression.This can cause unintended side effects if the expression is a function call where the returned value changes fromundefined to some other value.

For example, ifdoSomething() returnsfalse in the code below, the checkbox will no longer be marked as checked or unchecked when the checkbox is clicked (returningfalse from the handler disables the default action).

js
checkbox.onclick = () => doSomething();

This is unlikely to be desired behavior!To be safe, when the return value of a function is not intended to be used, it can be passed to thevoid operator to ensure that (for example) changing APIs do not cause arrow functions' behaviors to change.

js
checkbox.onclick = () => void doSomething();

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-void-operator

Browser compatibility

See also


[8]ページ先頭

©2009-2025 Movatter.jp