Javascript while loop return value

后端 未结 3 1299
悲&欢浪女
悲&欢浪女 2020-11-27 07:48

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

相关标签:
3条回答
  • 2020-11-27 08:40

    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.

    0 讨论(0)
  • 2020-11-27 08:45

    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.

    0 讨论(0)
  • 2020-11-27 08:45

    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 讨论(0)
提交回复
热议问题