最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What does return void 0 === i && (i = 3), 0 === i ? ( ..A.. ) : ( ..B.. ) do? - Stack Overflow

programmeradmin5浏览0评论

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 ma? I've checked this question but I still don't get it.

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

Thank you

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 ma? I've checked this question but I still don't get it.

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

Thank you

Share Improve this question edited Jun 10, 2022 at 15:43 miken32 42.8k16 gold badges125 silver badges174 bronze badges asked Feb 2, 2017 at 10:14 Giao ScarpinoGiao Scarpino 6476 silver badges17 bronze badges 4
  • 1 I guess it just executes (..B..) block of statements whatever i value is passed. – pttsky Commented Feb 2, 2017 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 Commented Feb 2, 2017 at 10:18
  • 0 === i checks whether i is 0 and then does either (..A..) or (..B..). This return uses the ma and ternary operations. – evolutionxbox Commented Feb 2, 2017 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 Commented Feb 2, 2017 at 10:20
Add a ment  | 

4 Answers 4

Reset to default 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 parison 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 ma 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 parison of 0 and i. Means if i is 0 at this point, the return value of the whole expression is true.

As last there es 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 )
}

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));

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.

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;
  • ma 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.
发布评论

评论列表(0)

  1. 暂无评论