-2

This function

function mapForEach(arr, fn) {

  var newArr = [];
  for (var i = 0; i < arr.length; i++) {
    newArr.push(
      fn(arr[i])
    )
  };

  return newArr;
}

var arr1 = [1, 2, 3];

is invoked by

mapForEach(arr1, function (item) {
  return item * 2;
}); // [2,4,6]

and is not invoked by this variable declaration

var arr2 = mapForEach(arr1, function (item) {
  return item * 2;
});

yet is invoked when logged to the console

console.log(arr2); // [2,4,6] in the console

why isn't invocation here necessary?

console.log(arr2());

I'm still not clear how this differs from

function foo () { return 2 === 2 };

logs function definition

console.log(foo); // function foo() { return 2 === 2 } 

invoked in log function, logs return value

console.log(foo()); // true
nSideOut
  • 29
  • 2
  • 7
    Because it *is* invoked by that variable declaration. Why do you think otherwise? You're just assigning the result to the name `arr2`. If you thought you were assigning a function, why did you name it an array? – jonrsharpe Feb 12 '17 at 23:07
  • I did not name it, I'm reading it and trying to learn, a process which has to happen unless you are born with perfect knowledge of Javascript (I wasn't). I thought that the assignment of the return value does not exist in memory until execution which had to happen now after the declaration as arr2() which then synchronously would be available down line in the execution of the program. So I was expecting console.log(arr2) to log a function definition. Luckily we have things like stack overflow where we can ask questions we don't already have the answers to, as opposed to questions we do...? – nSideOut Feb 13 '17 at 00:58
  • It does not help you to be snarky. The question "why do you think otherwise" is exploratory, to identify where your misconceptions lie, so we can address them directly, instead of just pointing you to read a book (which should address *all* of your misconceptions, whatever they are, but is also almost uselessly general for a specific issue you are having). – Amadan Feb 13 '17 at 01:20
  • I wasn't replying to _"Why do you think otherwise"_ but _"If you thought you were assigning a function, why did you name it an array?"_ Why assume I named it? If I had named it, and by naming it as 'array' implied that I knew the answer to my question, why would I ask my question? I don't mean to be snarky, I've just seen so many people get bashed here for asking questions. Isn't that what it's for? When i can differentiate a legitimate question from an illegitimate question or a question appropriate for a book to answer from one for a person to answer, I'll do so. – nSideOut Feb 13 '17 at 02:08
  • You did name it `var arr2 = ...`, that's what I was referring to. – jonrsharpe Feb 13 '17 at 07:17

1 Answers1

0

I think what's happening here is just a slight confusion over the behaviour in the JavaScript console or in the return value from a var declaration.

The console will always display the value of the thing you type in. e.g. the return value of the call to a function. However the return value of a var is undefined. This means that while the mapForEach without an assignment will display [2,4,6] the function is indeed being called in both cases, it's just with var arr2 = the thing you were expecting is not displayed.

See this question for some more background on the return value from a var declaration.

Community
  • 1
  • 1
mikej
  • 65,295
  • 17
  • 152
  • 131
  • 1
    Slight correction: "the return value of an assignment is `undefined`" is not true (`arr2 = [2, 4, 6]` evaluates to `[2, 4, 6]`); "the return value of a `var` statement is `undefined`" is closer to the truth (`var arr = [2, 4, 6]` evaluates to `undefined` in console, and cannot even be an lvalue in a program). – Amadan Feb 13 '17 at 01:23
  • Many thanks @Amadan - I had a feeling my answer wasn't spot on given the contexts where the value of an assignment can be used, however was trying to explain based on the examples from the OP. Will update the answer to clarify! – mikej Feb 13 '17 at 10:11