te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Javascript while loop return value - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript while loop return value - Stack Overflow

programmeradmin2浏览0评论

I have a simple question regarding while loop in Javascript. When I run this simple loop in browser console:

var count = 0;
while (count < 10) {
    console.log(count);
    count++;
}

I have a simple question regarding while loop in Javascript. When I run this simple loop in browser console:

var count = 0;
while (count < 10) {
    console.log(count);
    count++;
}

The output to console log is 0,1,2...9. (as expected). However there is one more number which is returned to the console:

<- 9

Where does this return value e from?

I assume this is the return value of count++ expression. But why is the value not returned by every single loop?

Is it possible to somehow catch that returned value to a variable?

Share Improve this question edited Feb 17, 2016 at 10:47 Giordano 5,5703 gold badges36 silver badges51 bronze badges asked Feb 17, 2016 at 10:39 sachadsachad 3075 silver badges13 bronze badges 3
  • I am getting only 0-9 – PrakashSharma Commented Feb 17, 2016 at 10:42
  • 3 @PrakashSharma Paste it straight into the console - the OP is talking about the value displayed after running the code. – James Thorpe Commented Feb 17, 2016 at 10:45
  • Please follow this link, it may help you. stackoverflow./questions/31434942/… – Sarge Commented Mar 25, 2016 at 8:07
Add a ment  | 

3 Answers 3

Reset to default 10

Read-eval-print-loops (REPLs) like browser consoles show the last result that the code generated. Somewhat surprisingly, JavaScript while loops and blocks have a result. For blocks, it's the result of the last statement in the block. For while statements, it's the result of the last iteration of the statement attached to the while (a block in your case).

Here's a simpler example with just a block:

{1 + 1; 3 + 3;}

In a REPL like the browser console, the above will show you 6, because it's a block containing two ExpressionStatements. The result of the first ExpressionStatement is 2 (that result is thrown away), and the result of the second one is 6, so the result of the block is 6. This is covered in the specification:

  1. Let blockValue be the result of evaluating StatementList.
  2. Set the running execution context’s LexicalEnvironment to oldEnv.
  3. Return blockValue.

while loop results are covered here.

Is it possible to somehow catch that returned value to a variable?

No, these results are not accessible in the code. E.g., you can't do var x = while (count < 10 { count++; }); or similar. You'd have to capture the result you want inside the loop/block/etc., assigning it to a variable or similar. Or (and I'm not suggesting this), use eval as Alin points out.

When you run code directly in the browser console, it runs the code then logs the value of the last expression executed, in this case the value of count++, which at the final execution is 9 (it gets changed to 10 with the post-increment operator, ie after the value 9 is "read").

If you changed your code to:

var count = 0;
while (count < 10) {
  console.log(count);
  ++count;
}

It would instead log <- 10.

But why is the value not returned by every single loop?

Because it's not being returned at all, it's just the behaviour of the console.

Is it possible to somehow catch that returned value to a variable?

Yes, but you'll need to assign it:

var count = 0;
var lastcount;
while (count < 10) {
  console.log(count);
  lastcount = count++;
}

console.log(lastcount);

Note that if you run this snippet in the console, you'll get two 9s logged (one from the final loop, one from the extra console.log) followed by <- undefined, because the last console.log returns undefined.

I think the answer can be found in the behavior of eval:

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

What you throw in the console is most probably put through an eval() (or something similar). And the last evaluated expression, in your case count++ is returned and then logged to the console. (the value is 9, and not 10, because of post-incrementation).

I don't think you can store the result of the evaluation, unless you explicitly put it through another eval().

var x = eval("var count = 0;while (count < 10) {console.log(count);count++;};");

Or, in the Google Chrome console, you can use $_.

发布评论

评论列表(0)

  1. 暂无评论