3

I came to this code but I don't understand very well what it does..

test.update = function(i) 
{ 
return void 0 === i && (i = 3), 0 === i ? (..A..) : (..B..) 
}

(..A..) and (..B..) are just other lines of code I haven't posted.

Let's say if i would have a 0 value, what the function will return?

What does "void 0 === i && (i = 3)" do? Specially (i = 3). Does that mean that if (void 0 === i) is true and i can get 3, i will be 3? And what about the comma? I've checked this question but I still don't get it.

Sorry for so many questions but I'd like to have a complete answer so I can totally understand what is going on.

Thank you

miken32
  • 42,008
  • 16
  • 111
  • 154
Giacomo Scarpino
  • 599
  • 3
  • 17
  • 1
    I guess it just executes `(..B..)` block of statements whatever `i` value is passed. – pttsky Feb 02 '17 at 10:17
  • `void 0 === i` seems to check whether `i` is `undefined`. `i = 3` sets `i` to the number 3 when `i` in not undefined. – evolutionxbox Feb 02 '17 at 10:18
  • `0 === i` checks whether `i` is `0` and then does either `(..A..)` or `(..B..)`. This return uses the [comma](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator) and [ternary operations](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator). – evolutionxbox Feb 02 '17 at 10:19
  • 2
    if that can help `void expression` is used when you want to evaluate the expression, but that the value returned is always `undefined` – Kaddath Feb 02 '17 at 10:20

4 Answers4

5

Okay, first let's set brackets according to operator precedence:

return (((void 0) === i) && (i = 3)), ((0 === i) ? A : B)

Now to the single operations

void 0

simply returns undefined. We could also write it this way:

undefined === i

which obviously checks whether i is undefined.

i = 3

looks like a comparison first, but in fact it's an assignment that returns 3. So far the line looks up whether i is undefined and in case it is, it is assigned the value 3.

Now the following comma is an operator on its own. It evaluates all expressions from left to right and returns the last one (right-most). In this case the last expression is a comparison of 0 and i. Means if i is 0 at this point, the return value of the whole expression is true.

As last there comes a conditional operator which is a short way to write if .. else ...

So far the line could have been also written as:

if (i === undefined) {
  i = 3;
}
if (i === 0) {
  // return ( A )
}
else {
  // return ( B )
}
Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74
2

Just run it and see that the first thing is returning undefined no matter what the rest does

if i ===0, run A, else run B and if i =="", i=3 -> run b

var test= {}
test.update = function(i)  { 
  return void 0 === i && (i = 3), 0 === i ? (console.log(i,"a")) : (console.log(i,"b"))
}

console.log("i is null, res:"+test.update(null));
console.log("no i, res:"+test.update());
console.log("i blank, res:"+test.update(""));
console.log("i=0,res:"+test.update(0));
console.log("i=1,res:"+test.update(1));
console.log("i=2,res:"+test.update(2));
console.log("i=3,res:"+test.update(3));
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

The rest is important, because the void operator evaluates the expression and returns undefined.

For example if the expression is a function call, like

return void func();

then the function is called and void returns undefined, even if func is returning something different.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

I guess it just executes (..B..) block of statements whatever i value is passed.

  • void operator evaluates its operand and then returns undefined not depending of result of evaluation;
  • comma operator just evaluates one thing, then another, left to right (like in var i = 0, j = 0;)
  • 0 === i && (i = 3) sets i to 3 if i equals 0
  • 0 === i ? (..A..) : (..B..) executes A or B block of statements, but as said above, it is guaranteed at this moment that i is not equal to 0.
pttsky
  • 737
  • 5
  • 15