What is void in javascript?

Asked

Viewed 7,520 times

10

I could contemplate a javascript code that had the following code:

if (context === void 0) {
   // faça algo
}

I don’t understand what that chunk of code means or what it does.

The one time I used the void was to create blind links

Thus:

<a href="javascript:void(0);">Link Cego</a>

After all:

  • What is void in javascript?

  • What is being verified in the first example, where the void 0 used in comparison? Wouldn’t be the same thing that typeof context == 'undefined'?

3 answers

11


It is an operator built to allow side effects in places where an expression is desired that evaluates to undefined, but without the direct use of this global variable. void expr has the same equivalence as void(expr), and is the lowest pure expression that evaluates to undefined. When used immediately after an immediate function invocation expression, it can be used to force the keyword itself to be treated as an expression. What makes creating blind links in JS is that by evaluating to undefined, a redirect to the browser is rejected. Mozilla has good documentation, which, with a little bit of effort, can be useful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void.

In his example, one could compare to typeof context === 'undefined' or context === undefined.

2

The operator void evaluates an expression such as undefined. in the case void 0, it evaluates the value 0 as Undefined. In this case void 1, void 2, void (1 + 2) and void Math.round(12.5), both are evaluated as undefined. In the case of the example it is comparing the value of the context variable with Undefined. Using void 0 is a convention, I honestly don’t know why it.

You have to stay tuned to the order of precedence of the operator, because void (1 + 2) produces undefined, meanwhile void 1 + 2 produces NaN.

-10

This operator allows the insertion of expressions that produce side effects in places where an expression that evaluates the action {{jsxref("Global_objects/Undefined", "undefined")}} is desired.

The void operator is often used only to obtain the undefined primitive value, usually using "void(0)" (which equates to "void 0"). In these cases, the global variable {{jsxref("Global_objects/Undefined", "undefined")}} can be used instead (assuming it has not been assigned to a non-standard value).

Immediate call of function expressions When we use an Instant Call of function expressions, null values can be used to force the keyword of the function to be treated as an expression instead of a declaration.

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

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

Javascript Uris When a browser Follows a javascript: URI, it evaluates the code in the URI and then Replaces the Contents of the page with the returned value, unless the returned value is {{jsxref("Global_objects/Undefined", "Undefined")}}. The void Operator can be used to Return {{jsxref("Global_objects/Undefined", "Undefined")}}. For example:

<a href="javascript:void(0);">
  Clique aqui para não fazer nada
</a>

<a href="javascript:void(document.body.style.backgroundColor='green');">
  Clique aqui para o papel de parede ser verde
</a>
  • 10

    When the contents are copied from somewhere, it is interesting to inform the source in this case, the MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void). Additionally, your reply contains untranslated content.

  • Sorry I’m new here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.