2

I came across this question in one of the online quizes. The solution I came up with was function. BUT, the console logs number.

var f = (
   function f(){return "1";},
   function g (){return 2;}
)();
console.log(typeof f);

My thinking was: var f becomes an IIFE, so typeof f should be a function.

Now, apparently, function g() inside var f executes and returns its value to var f, so the console logs number (or 2, if I just log f).

Questions:

1.What is the explanation for function comma anotherFunction. This is not an object, and also not a variable declaration (like var x, y;)?

2.Somehow the second function is the one that is being returned, but what part of the code is actually executing it?

(I thought that () just before the console.log call is executing the outer f function (which should just define inner functions f() and g(), but not execute any of them ). The second one gets executed, so , obviously, I was wrong.

Yozex
  • 73
  • 8

0 Answers0