Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 1.48 KB

File metadata and controls

42 lines (30 loc) · 1.48 KB

void operator in JavaScript

In JavaScript, void is a unary operator that evaluates an expression and returns undefined. It is primarily used to ensure that an expression evaluates to undefined regardless of the original value or side effects of the expression.

Syntax

void expression

Common Uses of void

  1. Ensuring undefined Return Value: Using void can be useful in contexts where you need an expression to explicitly return undefined. For example:

    undefined = 3;
    void 0; // returns undefined
    void (0); // returns undefined
    void "hello"; // returns undefined
  2. Bookmarklets: In JavaScript bookmarklets (small JavaScript programs stored as a URL), using void can prevent the browser from navigating to a new URL when the code is executed. Try this in your browser's address bar:

    javascript:void(alert('Hello, World!'));
  3. Self-invoking Functions: void can be used with self-invoking functions (Immediately Invoked Function Expressions or IIFEs) to ensure they return undefined:

     > void (function() { return 1; })()
     undefined
     > (function() { return 1; })()
     1
  4. Avoiding JavaScript Engine Optimization Issues: In rare cases, void is used to prevent JavaScript engines from applying certain optimizations that could cause unexpected behavior, although this is more of a historical artifact and not common practice today.