このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。
void 演算子
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
void 演算子は与えられた式 (expression) を評価し、undefined を返します。
In this article
試してみましょう
const output = void 1;console.log(output);// 予想される結果: undefinedvoid console.log("式が評価されました");// 予想される結果: "式が評価されました"void (function iife() { console.log("IIFE が実行されました");})();// 予想される結果: "IIFE が実行されました"void function test() { console.log("test 関数が実行されました");};try { test();} catch (e) { console.log("test 関数が定義されていません"); // 予想される結果: "test 関数が定義されていません"}構文
void expression解説
この演算子により、何らかの値を生成する評価式を、式がundefined と評価されることが望ましい場面に置くことができます。
void 演算子は、よく単にプリミティブ値undefined を得る目的で使われ、一般的に "void(0)" と書かれます(これは "void 0" と等価です)。この目的であれば、グローバル変数undefined を使用することができます。
なお、void 演算子の優先順位を考慮するべきであり、括弧はvoid 演算子に続く式の解決を明確にするのに役立つとされています。
void 2 === "2"; // (void 2) === '2', false を返すvoid (2 === "2"); // void (2 === '2'), undefined を返す例
>即時実行関数式
即時実行関数式が使用される場合、function キーワードは文のすぐ先頭に置くことはできません。そうすると、関数宣言 と解釈され、呼び出しを表す括弧に達した時点で構文エラーが発生します。関数に名前が付けられていない場合、関数が宣言として構文解析されると即座に構文エラーとなります。
function iife() { console.log("実行されました!");}(); // SyntaxError: Unexpected token ')'function () { console.log("実行されました!");}(); // SyntaxError: Function statements require a function name関数を式として構文解析するためには、function キーワードが、文ではなく式のみを受け入れる位置に現れる必要があります。これは、キーワードの前に単項演算子を付けることで実現することができます。単項演算子はオペランドとして式のみを受け入れます。関数呼び出しは単項演算子よりも優先順位が高いため、最初の(すなわち最も重要な)操作として実行されます。その返値(ほとんどの場合undefined)は単項演算子に渡すと、直ちに破棄されます。
単項演算子の中で、void が最もよい意味づけを提供します。関数呼び出しの返値を破棄すべきことを明確に示すからです。
void function () { console.log("実行されました!");}();// Logs "実行されました!"これは関数式を括弧で囲むよりも少し長いですが、同じ効果が得られます。つまり、function キーワードが文の開始ではなく式の開始として解析されるように強制するのです。
(function () { console.log("実行されました!");})();ただし、このテクニックはfunction キーワードで定義されたIIFEにのみ適用されます。アロー関数で括弧を避けるためにvoid 演算子を使用しようとすると、構文エラーが発生します。アロー関数式は呼び出される際に常に括弧で囲む必要があります。
void () => { console.log("iife!"); }(); // SyntaxError: Malformed arrow function parameter listJavaScript URI
#"/ja/docs/Web/JavaScript/Reference/Global_Objects/undefined"> でない限り、返された値でページの内容を置き換えます。undefinedvoid 演算子は、undefined を返すために使用することができます。
<a href="#">ここをクリックしても何もしません</a><a href="#"> ここをクリックすると背景が緑色になります</a>メモ: アロー関数は略式の中括弧のない構文を導入しており、式を返します。これは、式が関数呼び出しであり、返値が 例えば、下記コードで これは望ましくない動作です。安全のため、関数の返値が使用されない場合は、#"アロー関数からのリーク防止" >アロー関数からのリーク防止
undefined から他の値に変更された場合に、意図しない副作用が発生する可能性があります。doSomething() がfalse を返す場合、チェックボックスをクリックしてもチェック状態が更新できなくなりました (ハンドラーからfalse を返すことで既定の措置が無効化されるため)。checkbox.onclick = () => doSomething();void 演算子に渡すと、(例えば) API の変更によってアロー関数の動作が変わらないように保証できます。checkbox.onclick = () => void doSomething();仕様書
Specification ECMAScript® 2026 Language Specification>
# sec-void-operator>ブラウザーの互換性
関連情報
- JavaScript
- チュートリアル
- リファレンス
組み込みオブジェクト
- AggregateError
- Array
- ArrayBuffer
- AsyncDisposableStack
- AsyncFunction
- AsyncGenerator
- AsyncGeneratorFunction
- AsyncIterator
- Atomics
- BigInt
- BigInt64Array
- BigUint64Array
- Boolean
- DataView
- Date
- decodeURI()
- decodeURIComponent()
- DisposableStack
- encodeURI()
- encodeURIComponent()
- Error
- escape()非推奨;
- eval()
- EvalError
- FinalizationRegistry
- Float16Array
- Float32Array
- Float64Array
- Function
- Generator
- GeneratorFunction
- globalThis
- Infinity
- Int8Array
- Int16Array
- Int32Array
- InternalError非標準
- Intl
- isFinite()
- isNaN()
- Iterator
- JSON
- Map
- Math
- NaN
- Number
- Object
- parseFloat()
- parseInt()
- Promise
- Proxy
- RangeError
- ReferenceError
- Reflect
- RegExp
- Set
- SharedArrayBuffer
- String
- SuppressedError
- Symbol
- SyntaxError
- Temporal
- TypedArray
- TypeError
- Uint8Array
- Uint8ClampedArray
- Uint16Array
- Uint32Array
- undefined
- unescape()非推奨;
- URIError
- WeakMap
- WeakRef
- WeakSet
式と演算子
- 加算演算子 (+)
- 加算代入演算子 (+=)
- 代入演算子 (=)
- async function 式
- async function* 式
- await 演算子
- ビット論理積演算子 (&)
- ビット論理積代入演算子 (&=)
- ビット否定演算子 (~)
- ビット論理和演算子 (|)
- ビット論理和代入演算子 (|=)
- ビット排他的論理和 (^)
- ビット排他的論理和代入演算子 (^=)
- class 式
- カンマ演算子 (,)
- 条件(三項)演算子
- デクリメント演算子 (--)
- delete 演算子
- 構造分解(分割代入)
- 除算演算子 (/)
- 除算代入演算子 (/=)
- 等価演算子 (==)
- べき乗演算子 (**)
- べき乗代入演算子 (**=)
- function 式
- function* 式
- 大なり演算子 (>)
- 大なりイコール演算子 (>=)
- グループ化演算子 ( )
- import.meta
- import()
- in 演算子
- インクリメント演算子 (++)
- 不等価演算子 (!=)
- instanceof
- 左シフト演算子 (<<)
- 左シフト代入演算子 (<<=)
- 小なり演算子 (<)
- 小なりイコール演算子 (<=)
- 論理積演算子 (&&)
- 論理積代入演算子 (&&=)
- 論理否定演算子 (!)
- 論理和演算子 (||)
- 論理和代入演算子 (||=)
- 乗算 (*)
- 乗算代入演算子 (*=)
- new 演算子
- new.target
- null
- ヌル値合体代入演算子 (??=)
- ヌル値合体演算子 (??)
- オブジェクト初期化子
- 演算子の優先順位
- オプショナルチェーン演算子 (?.)
- プロパティアクセサー
- 剰余 (%)
- 剰余代入演算子 (%=)
- 右シフト演算子 (>>)
- 右シフト代入演算子 (>>=)
- スプレッド構文 (...)
- 厳密等価演算子 (===)
- 厳密不等価演算子 (!==)
- 減算 (-)
- 減算代入演算子 (-=)
- super
- this
- typeof 演算子
- 単項マイナス演算子 (-)
- 単項プラス演算子 (+)
- 符号なし右シフト演算子 (>>>)
- 符号なし右シフト代入演算子 (>>>=)
- void 演算子
- yield 演算子
- yield* 演算子
正規表現
- 後方参照: \1, \2
- キャプチャグループ: (...)
- 文字クラスエスケープ: \d, \D, \w, \W, \s, \S
- 文字クラス: [...], [^...]
- 文字エスケープ: \n, \u{...}
- 論理和: |
- 入力境界アサーション: ^, $
- リテラル文字: a, b
- 先読みアサーション: (?=...), (?!...)
- 後読みアサーション: (?<=...), (?<!...)
- 修飾子: (?ims-ims:...)
- 名前付き後方参照: \k<name>
- 名前付きキャプチャグループ: (?<name>...)
- 非キャプチャグループ: (?:...)
- 数量詞: *, +, ?, {n}, {n,}, {n,m}
- Unicode 文字クラスエスケープ: \p{...}, \P{...}
- ワイルドカード: .
- 単語境界アサーション: \b, \B
エラー
- AggregateError: No Promise in Promise.any was resolved
- Error: Permission denied to access property "x"
- InternalError: too much recursion
- RangeError: argument is not a valid code point
- RangeError: BigInt division by zero
- RangeError: BigInt negative exponent
- RangeError: form must be one of 'NFC', 'NFD', 'NFKC', or 'NFKD'
- RangeError: invalid array length
- RangeError: invalid date
- RangeError: precision is out of range
- RangeError: radix must be an integer
- RangeError: repeat count must be less than infinity
- RangeError: repeat count must be non-negative
- RangeError: x can't be converted to BigInt because it isn't an integer
- ReferenceError: "x" is not defined
- ReferenceError: assignment to undeclared variable "x"
- ReferenceError: can't access lexical declaration`X' before initialization
- ReferenceError: must call super constructor before using 'this' in derived class constructor
- ReferenceError: super() called twice in derived class constructor
- SyntaxError: 'arguments'/'eval' can't be defined or assigned to in strict mode code
- SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated
- SyntaxError: "use strict" not allowed in function with non-simple parameters
- SyntaxError: "x" is a reserved identifier
- SyntaxError: \ at end of pattern
- SyntaxError: a declaration in the head of a for-of loop can't have an initializer
- SyntaxError: applying the 'delete' operator to an unqualified name is deprecated
- SyntaxError: arguments is not valid in fields
- SyntaxError: await is only valid in async functions, async generators and modules
- SyntaxError: await/yield expression can't be used in parameter
- SyntaxError: cannot use `??` unparenthesized within `||` and `&&` expressions
- SyntaxError: character class escape cannot be used in class range in regular expression
- SyntaxError: continue must be inside loop
- SyntaxError: duplicate capture group name in regular expression
- SyntaxError: duplicate formal argument x
- SyntaxError: for-in loop head declarations may not have initializers
- SyntaxError: function statement requires a name
- SyntaxError: functions cannot be labelled
- SyntaxError: getter and setter for private name #x should either be both static or non-static
- SyntaxError: getter functions must have no arguments
- SyntaxError: identifier starts immediately after numeric literal
- SyntaxError: illegal character
- SyntaxError: import declarations may only appear at top level of a module
- SyntaxError: incomplete quantifier in regular expression
- ReferenceError: invalid assignment left-hand side
- SyntaxError: invalid BigInt syntax
- SyntaxError: invalid capture group name in regular expression
- SyntaxError: invalid character in class in regular expression
- SyntaxError: invalid class set operation in regular expression
- SyntaxError: invalid decimal escape in regular expression
- SyntaxError: invalid identity escape in regular expression
- SyntaxError: invalid named capture reference in regular expression
- SyntaxError: invalid property name in regular expression
- SyntaxError: invalid range in character class
- SyntaxError: invalid regexp group
- SyntaxError: invalid regular expression flag "x"
- SyntaxError: invalid unicode escape in regular expression
- SyntaxError: JSON.parse: bad parsing
- SyntaxError: label not found
- SyntaxError: missing : after property id
- SyntaxError: missing ) after argument list
- SyntaxError: missing ) after condition
- SyntaxError: missing ] after element list
- SyntaxError: missing } after function body
- SyntaxError: missing } after property list
- SyntaxError: missing = in const declaration
- SyntaxError: missing formal parameter
- SyntaxError: missing name after . operator
- SyntaxError: missing variable name
- SyntaxError: negated character class with strings in regular expression
- SyntaxError: new keyword cannot be used with an optional chain
- SyntaxError: nothing to repeat
- SyntaxError: numbers out of order in {} quantifier.
- SyntaxError: octal escape sequences can't be used in untagged template literals or in strict mode code
- SyntaxError: parameter after rest parameter
- SyntaxError: private fields can't be deleted
- SyntaxError: property name __proto__ appears more than once in object literal
- SyntaxError: raw bracket is not allowed in regular expression with unicode flag
- SyntaxError: redeclaration of formal parameter "x"
- SyntaxError: reference to undeclared private field or method #x
- SyntaxError: rest parameter may not have a default
- SyntaxError: return not in function
- SyntaxError: setter functions must have one argument
- SyntaxError: unterminated string literal
- SyntaxError: super() is only valid in derived class constructors
- SyntaxError: tagged template cannot be used with optional chain
- SyntaxError: Unexpected '#' used outside of class body
- SyntaxError: Unexpected token
- SyntaxError: unlabeled break must be inside loop or switch
- SyntaxError: unparenthesized unary expression can't appear on the left-hand side of '**'
- SyntaxError: use of super property/member accesses only valid within methods or eval code within methods
- SyntaxError: Using //@ to indicate sourceURL pragmas is deprecated. Use //# instead
- ReferenceError: deprecated caller or arguments usage
- TypeError: 'x' is not iterable
- TypeError: "x" is (not) "y"
- TypeError: "x" is not a constructor
- TypeError: "x" is not a function
- TypeError: "x" is not a non-null object
- TypeError: "x" is read-only
- TypeError: already executing generator
- TypeError: BigInt value can't be serialized in JSON
- TypeError: calling a builtin X constructor without new is forbidden
- TypeError: can't access/set private field or method: object is not the right class
- TypeError: can't assign to property "x" on "y": not an object
- TypeError: can't convert BigInt to number
- TypeError: can't convert x to BigInt
- TypeError: can't define property "x": "obj" is not extensible
- TypeError: can't delete non-configurable array element
- TypeError: can't redefine non-configurable property "x"
- TypeError: can't set prototype of this object
- TypeError: can't set prototype: it would cause a prototype chain cycle
- TypeError: cannot use 'in' operator to search for 'x' in 'y'
- TypeError: class constructors must be invoked with 'new'
- TypeError: cyclic object value
- TypeError: derived class constructor returned invalid value x
- TypeError: getting private setter-only property
- TypeError: Initializing an object twice is an error with private fields/methods
- TypeError: invalid 'instanceof' operand 'x'
- TypeError: invalid Array.prototype.sort argument
- TypeError: invalid assignment to const "x"
- TypeError: Iterator/AsyncIterator constructor can't be used directly
- TypeError: matchAll/replaceAll must be called with a global RegExp
- TypeError: More arguments needed
- TypeError: "x" has no properties
- TypeError: property "x" is non-configurable and can't be deleted
- TypeError: Reduce of empty array with no initial value
- TypeError: setting getter-only property "x"
- TypeError: WeakSet key/WeakMap value 'x' must be an object or an unregistered symbol
- TypeError: X.prototype.y called on incompatible type
- URIError: malformed URI sequence
- Warning: -file- is being assigned a //# sourceMappingURL, but already has one
- Warning: unreachable code after return statement