Javascript while loop return value

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

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

The output to the console log is 0.1.2 ... 9. (as expected). However, there is another number that returns to the console:

 <- 9 

Where does this return value come from?

I assume this is the return value of count++ expression . But why doesn't the value return in every single loop?

Is there any way to catch this return value of a variable?

+5
javascript syntax loops while-loop
source share
3 answers

Read-eval-print-loops (REPL), such as browser consoles, show the last result that the code generates. Somewhat surprisingly, JavaScript while loops and blocks have a result. For blocks, this is the result of the last statement in the block. For while statements, this is the result of the last iteration of the statement attached to the while (block in your case).

Here is a simpler example with only a block:

 {1 + 1; 3 + 3;} 

In REPL, such as a browser console, the above will show you 6 because it is a block containing two ExpressionStatements . The result of the first ExpressionStatement expression is 2 (the result is discarded), and the result of the second is 6, so the result of the block is 6. This is in the specification :

  1. Let blockValue be the result of evaluating a StatementList.
  2. Set the current LexicalEnvironment execution contexts to oldEnv.
  3. Return blockValue.

while results are described here .

Is there any way to catch this return value of a variable?

No, these results are not available in the code. For example, you cannot do var x = while (count < 10 { count++; }); or the like. You will need to capture the result you want inside the loop / block / etc., Assigning it to a variable or the like. Or (and I do not suggest this), use eval as Alin points out .

+7
source share

When you run the code directly in the browser console, it runs the code, then it registers the value of the last expressed expression, in this case the value is count++ , which is 9 at the final execution (it changes to 10 with the operator after the increment, i.e. after the value 9 "read").

If you changed your code to:

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

Instead, there will be log <- 10 .

But why doesn't the value return in every single loop?

Since it doesn't return at all, this is just console behavior.

Is there any way to catch this return value of a variable?

Yes, but you 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 will get two 9 in the log (one from the last loop, one from the additional console.log ), followed by <- undefined , because the last console.log returns undefined .

+2
source share

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

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

What you throw into the console is most likely through eval() (or something similar). And the last evaluated expression, in your case, count++ returned and then registered on the console. (value 9 , not 10 , due to post-incrementation).

I do not think that you can save the result of the evaluation if you do not explicitly pass it through another eval() .

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

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

+1
source share

All Articles