admin管理员组文章数量:1024074
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++
.
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++
.
本文标签: javascriptChrome DevTools what39s this arrow(lt) meaningStack Overflow
版权声明:本文标题:javascript - Chrome DevTools: what's this arrow(<-) meaning? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745486968a2152786.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论