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 usex = while(i < 5) { i++ }
to capture this value, but you can capture it by usingx = eval('while (i < 5) { i++ }')
, which assigns4
tox
. 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
1 Answer
Reset to default 7This 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条)