Curse feedback (inline function, function to variable assignment)

Tell us what’s happening:
I did not understand following piece of article:

var printNumTwo;
for (var i = 0; i < 3; i++) {
  if (i === 2) {
    printNumTwo = function() {
      return i;
// returns 3

There was not yet in the curse information, that it is possible to store function in a variable, and how such a construction works.
This example is really surprising for me.

After some experiments with running the code sample, I hope finally understand the behavior, but it was quite misleading piece. (Inline function assigned to a variable without any explanation…).
Thanks :slight_smile:

Link to the challenge:

I think you mean “Course” feedback.
The challenge is there to show the effect of the let declaration within the scope of your code.
I hope you understand it better now and see the problem it solves.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


It’s a function expression. I agree it is a bit unfortunate this concept has not been introduced before it shows up in code challenges and example code.

When you get to the arrow function part of the ES6 curriculum you will see them used again. Although the focus seem to be on the anonymous part and not the expression part.

I looked for printNumTwo() (used in console.log()) function definition, and I could not find it. So I first thougt, that the code sample is wrong.
Maybe you can add the function expression link to that course article.
Have a nice day :slight_smile: