javascript - Chrome DevTools: what's this arrow(<-) meaning? - Stack Overflow

I am confuse about this symbol (<-) in Chrome DevToolsIt's return value or console value?When I

I am confuse about this symbol (<-) in Chrome DevTools

It's return value or console value?

When I run this while loop

var i = 0;
while (i < 5) {
    console.log(i);
    i++;
}

the console log spits out 4 twice, the last 4 have a (<-) in a front, what's meaning?

I am confuse about this symbol (<-) in Chrome DevTools

It's return value or console value?

When I run this while loop

var i = 0;
while (i < 5) {
    console.log(i);
    i++;
}

the console log spits out 4 twice, the last 4 have a (<-) in a front, what's meaning?

Share Improve this question asked Feb 17, 2014 at 4:26 PhonbopitPhonbopit 3,3833 gold badges23 silver badges29 bronze badges 3
  • 1 The arrow denotes the value the previous expression evaluates to, though I've no idea why a while loop is evaluating to a value. It's not syntactically valid to use x = while(i < 5) { i++ } to capture this value, but you can capture it by using x = eval('while (i < 5) { i++ }'), which assigns 4 to x. Pretty interesting. – user229044 Commented Feb 17, 2014 at 4:30
  • Weirder still, it evaluates to the last expression inside the loop body, even if the loop body doesn't return it: x = true; while (x) { x = false; "a" } // => "a" – user229044 Commented Feb 17, 2014 at 4:35
  • Also answered here stackoverflow./questions/14713320/… – nCardot Commented May 12, 2021 at 3:52
Add a ment  | 

1 Answer 1

Reset to default 7

This has to do with the nature of the eval function. Note that:

var i = 0, j = while(i < 5) { i++; };

Produces a pile error. However,

var i = 0, j = eval('while(i < 5) { i++; }');

Assigns the value 4 to j. Why is this? Quoting from MDN:

eval() returns the value of the last expression evaluated.

So in short, it evaluates all the calls to console.log in your expression, then also logs the return value from the eval-ed expression itself, which just happens to be the result of the last i++.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745486970a4629810.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信