JavaScript While Loop Return Value

Javascript, while loop return

return means end of function and return some value. Any statements after return statement will not be executed and the execution of a function will terminate at return statement. So, return in your case will make the loop to execute only one and terminate it.

Javascript while loop return value

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.

Does return stop a loop?

Yes, functions always end whenever their control flow meets a return statement.

The following example demonstrates how return statements end a function’s execution.

function returnMe() {
for (var i = 0; i < 2; i++) {
if (i === 1) return i;
}
}

console.log(returnMe());

Wait loops to return a value

You are overthinking the issue. Your problem is how you're implementing splice. What does the acc variable become as your while loop iterates. If your max is 100, do you agree that at some point, your acc will equal 99? Your splice would then be run as arr.splice(99, 1). That's an inverted index range, which has no proper meaning. JavaScript won't throw an error when you try and do that, because it is evil.

What you should be doing instead, is repeatedly splicing the first element off, which is at index 0. It is always at index 0, because you are mutating your array each time you run spice. This is the code you're looking for. I made some modifications, for brevity.

function a(max) {  const arr = [...Array(max)].map((_, idx) => idx)  const arrLength = arr.length  let acc = 0  while (acc < arrLength) {    arr.splice(0, 1)    acc++  }  return arr}
console.log(a(10000))

Why does this `do`–`while` loop repeat the last value after the end?

There is no extra 4 at end. Your loop is correct and works just fine :). That's return value of the i++ expression, which you are mistaking for console.log in developer console. Try this, you will figure it out;

var i=0;do{console.log('i = ', i);i++;}while(i<5);

Returning values out of for loop in javascript

You can do that with yield in newer versions of js, but that's out of question. Here's what you can do:

function getId(a){
var aL = a.length;
var values = [];
for(i = 0; i < aL; i++ ){
values.push(a[i][2].split(":", 1)[0]);
}
return values.join('');
}

How to return the product of a while loop in a variable

You were close:

var firstOne = 1;
var timesBy = 0;
var result1 = function () {
while (timesBy < 10) {
timesBy++;
firstOne = firstOne * timesBy;
}
return firstOne;
};
alert(result1());

FIDDLE



Related Topics



Leave a reply



Submit