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 ì°ì°ìë 주ì´ì§ ííìì íê°íê³ undefined를 ë°íí©ëë¤.
ìëí´ ë³´ê¸°
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"
}
구문
void expression;
ì¤ëª
voidë ê°ì ìì±íë ííìì íê°í´ì undefined를 ë°íí©ëë¤.
ì¤ì§ undefined ììê°ì ì»ê¸° ìí´ void 0 ëë void(0)ì²ë¼ ì¬ì©íë ê²½ì°ë ë³¼ ì ììµëë¤. ì´ë° ê²½ì° ì ì undefined를 ëì ì¬ì©í´ë ë©ëë¤.
void ì°ì°ìì ì°ì ììë ì ë
í´ì¼ í©ëë¤. 그룹 ì°ì°ì(ê´í¸)를 ì¬ì©íë©´ void ì°ì°ì를 ì¬ì©í ìì íê° ê³¼ì ì ë ëª
ííê² ë³´ì¼ ì ììµëë¤.
void 2 == "2"; // undefined == '2', false
void (2 == "2"); // void true, undefined
IIFE
ì¦ì ì¤í í¨ì ííì(IIFE)ì ì¬ì©í ë void를 ì¬ì©íë©´ function í¤ìë를 ì ì¸ë¬¸ì´ ìëë¼ ííìì²ë¼ ê°ì£¼íëë¡ ê°ì í ì ììµëë¤.
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를 ë°íí ì ììµëë¤. ë¤ì ìì 를 íì¸íì¸ì.
<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.
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 Language Specification > # sec-void-operator > |