0

Javascript's for statement returns undefined, at least when I use it in Chromium's JS repl:

> for (i=0;i<1;i++);
: undefined

Therefore, I would expect the following statement to interpret thusly:

> a = for (i=0;i<1;i++);
: undefined 

Instead I get

a = for (i=0;i<1;i++);
VM488:1 Uncaught SyntaxError: Unexpected token for

The only workaround I can think of is a = eval("for (i=0;i<1;i++);"), which does work. However, as my question states, I want to do this without using eval. Is it possible?

Alex V
  • 3,416
  • 2
  • 33
  • 52
  • Statements return nothing (by definition). – zerkms Nov 14 '18 at 21:42
  • 3
    `for` statements don't evaluate to a value at all. You're seeing `undefined` because you ran code which does not return a value. Even running that code with `eval` [results in a syntax error](https://jsfiddle.net/7sav8zb9/1/) – Mike Cluck Nov 14 '18 at 21:43
  • `Is it possible?` --- sure `var a = undefined; for (i=0;i<1;i++);` – zerkms Nov 14 '18 at 21:43
  • which value do you expect? – Nina Scholz Nov 14 '18 at 21:45
  • 1
    `eval("a = for (i=0;i<1;i++);")` gives me `SyntaxError: expected expression, got keyword 'for'` in Firefox. – Felix Kling Nov 14 '18 at 21:45
  • @FelixKling Oops! Sorry, meant to say a = eval(...). Updated question, thanks! – Alex V Nov 14 '18 at 21:52
  • @MikeCluck Oops, I meant to format it like, a = eval(...), which DOES assign undefined to variable a. Updated code in question! Thanks – Alex V Nov 14 '18 at 21:53
  • @alejandroalvarado Ahh, makes more sense. Still, you're getting `undefined` because no value is being returned from `eval`. Are you exclusively trying to get `undefined` or are you hoping to get some other kind of "return value" from a `for` loop? – Mike Cluck Nov 14 '18 at 21:54
  • @MikeCluck I am expecting it to return undefined. Here is my actual code: `i=(h)=>eval("for(a=0,b=1;h--;b=a+(a=b));")||a` I want to do this but without eval(). My motivation is I'm playing code golf with a friend, haha. – Alex V Nov 14 '18 at 21:57
  • The duplicate should answer your question, and provides a better example. The short answer is: No, there is no way to get the "implicit" return value of a statement/loop without `eval`. – Felix Kling Nov 14 '18 at 21:58
  • @alejandroalvarado The problem is that `for` statements do not evaluate to *any* value. Not even `undefined`. `eval`, on the other hand, returns `undefined` when given code that does not evaluate to a value or, of course, code that evaluates to `undefined` – Mike Cluck Nov 14 '18 at 21:58
  • 1
    @FelixKling Wow, awesome, thanks for the prompt help. Appreciate everyone who commented, learned something today! – Alex V Nov 14 '18 at 21:59
  • @MikeCluck Welp, guess that answers my question then. Thanks! – Alex V Nov 14 '18 at 21:59
  • @Mike: That's not quite right. See the duplicate and try `for (i=0;i<3; i++) i;` in the console. But sure, they are not expressions, so if that's what you mean then that's correct. – Felix Kling Nov 14 '18 at 22:00
  • Long story short: Any "procedure" in JavaScript returns some result (a *completion record*), mainly for control flow purposes (e.g. was an error thrown in the loop body). These kind of results can usually not be accessed directly. However, evaluating an expression basically means unwrapping the result to get actual value. https://www.ecma-international.org/ecma-262/9.0/index.html#sec-completion-record-specification-type – Felix Kling Nov 14 '18 at 22:10
  • @FelixKling Yeah, I got you. I was just meaning that `for` loops are not expressions so they don't have an accessible value. – Mike Cluck Nov 15 '18 at 12:48

1 Answers1

1

the for statement allowed you to iterate over a collection of items, which mean will be executing an instruction between the body of the for an example:

for (i=0;i<1;i++){
  //this is the body
  console.log(i);
}

the for statement doesn't return a value, what you are seeing is the dev chrome tools returning undefined.

read more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

what you might want to do is:

var myValues = []; // an array
for (i=0;i<5;i++){
  values.push(i)
}

console.log(values); // [0, 1, 2, 3, 4]
ncubica
  • 8,169
  • 9
  • 54
  • 72
  • *"of all function in javascript return undefined."* But there is no function here. – Felix Kling Nov 14 '18 at 21:53
  • I specifically want to put undefined inside of a variable AS A RESULT of a for statement, which can be done using eval() (as mentioned in question). My question is how to do it without resorting to eval()! – Alex V Nov 14 '18 at 21:55
  • @FelixKling I presume he meant to say all statements in JS return undefined? – Alex V Nov 14 '18 at 21:56
  • This answer is not quite right. Just try `for (i=0;i<3;i++) i;` in the console. See the duplicate of more info. – Felix Kling Nov 14 '18 at 21:59