[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/void [Back]  [Original]

void - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

void

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

void undefined .

In this article

const output = void 1;
console.log(output);
// Expected output: undefined

void 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"
}

js
void expression;

void undefined .

undefined void 0 void(0) . undefined .

void . () void .

js
void 2 == "2"; // undefined == '2', false
void (2 == "2"); // void true, undefined

IIFE

(IIFE) void function .

js
void (function iife() {
  var bar = function () {};
  var baz = function () {};
  var foo = function () {
    bar();
    baz();
  };
  var biz = function () {};

  foo();
  biz();
})();

JavaScript URI

javascript: URI , URI undefined . void undefined . .

html
<a href="javascript:void(0);">   </a>
<a href="javascript:void(document.body.style.backgroundColor='green');">
    
</a>

: javascript: .

Arrow functions introduce a short-hand braceless syntax that returns an expression. This can cause unintended side effects by returning the result of a function call that previously returned nothing. To be safe, when the return value of a function is not intended to be used, it can be passed to the void operator to ensure that (for example) changing APIs do not cause arrow functions' behaviors to change.

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

This ensures the return value of doSomething changing from undefined to true will not change the behavior of this code.

Specification
ECMAScript 2027 LanguageSpecification
# sec-void-operator


Web Proxy Viewer  |  New URL  |  Original Page