What is void in javascript?

7

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

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

I do not understand what this snippet of code means or what it does.

The only time I used void was to create blind links

So:

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

After all:

  • What is void in javascript?

  • What is being checked in the first example, where the void 0 used in the comparison? Would not it be the same as typeof context == 'undefined' ?

asked by anonymous 01.08.2015 / 18:57

3 answers

7

It is an operator built to allow side effects in places where an expression evaluating to undefined is desired, but without the direct use of this global variable . void expr has the same equivalence as void(expr) , and is the smallest 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 blind links in JS is that by evaluating undefined , a redirect to the browser is discarded. Mozilla has good documentation, which, with a little effort, can be useful: link .

In your example, you could compare typeof context === 'undefined' or context === undefined .

    
01.08.2015 / 19:25
1

The void operator evaluates an expression to undefined . in the void 0 case, it evaluates to 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 example case he is comparing the value of the context variable with undefined. Using void 0 is a convention, I honestly do not know why.

You have to be aware of the order of precedence of the operator, because void (1 + 2) produces undefined , however void 1 + 2 produces NaN .

    
03.08.2015 / 15:43
-9

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

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

Immediate call of function expressions When we use an immediate call of the function expressions, null values can be used to force the function keyword to be treated as an expression rather than a statement.

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>
    
02.08.2015 / 18:21