Which part of the code executes inner function? [duplicate]












2
















This question already has an answer here:




  • What does a comma do in JavaScript expressions?

    5 answers



  • What is the (function() { } )() construct in JavaScript?

    23 answers




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.










share|improve this question













marked as duplicate by melpomene, Nina Scholz javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 19:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • you should remove the space after the g

    – andrea-f
    Nov 21 '18 at 19:15






  • 1





    There is no "outer function".

    – melpomene
    Nov 21 '18 at 19:15






  • 1





    Who in their right mind would even write such a thing...

    – Ben Steward
    Nov 21 '18 at 19:16






  • 1





    maybe you have a look to the comma operator.

    – Nina Scholz
    Nov 21 '18 at 19:19






  • 1





    @NinaScholz That does not fully answer the question. Please read it in full. - "Somehow the second function is the one that is being returned, but what part of the code is actually executing it?"

    – Spencer Wieczorek
    Nov 21 '18 at 19:23


















2
















This question already has an answer here:




  • What does a comma do in JavaScript expressions?

    5 answers



  • What is the (function() { } )() construct in JavaScript?

    23 answers




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.










share|improve this question













marked as duplicate by melpomene, Nina Scholz javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 19:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • you should remove the space after the g

    – andrea-f
    Nov 21 '18 at 19:15






  • 1





    There is no "outer function".

    – melpomene
    Nov 21 '18 at 19:15






  • 1





    Who in their right mind would even write such a thing...

    – Ben Steward
    Nov 21 '18 at 19:16






  • 1





    maybe you have a look to the comma operator.

    – Nina Scholz
    Nov 21 '18 at 19:19






  • 1





    @NinaScholz That does not fully answer the question. Please read it in full. - "Somehow the second function is the one that is being returned, but what part of the code is actually executing it?"

    – Spencer Wieczorek
    Nov 21 '18 at 19:23
















2












2








2









This question already has an answer here:




  • What does a comma do in JavaScript expressions?

    5 answers



  • What is the (function() { } )() construct in JavaScript?

    23 answers




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.










share|improve this question















This question already has an answer here:




  • What does a comma do in JavaScript expressions?

    5 answers



  • What is the (function() { } )() construct in JavaScript?

    23 answers




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.





This question already has an answer here:




  • What does a comma do in JavaScript expressions?

    5 answers



  • What is the (function() { } )() construct in JavaScript?

    23 answers








javascript iife






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 19:13









YozexYozex

438




438




marked as duplicate by melpomene, Nina Scholz javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 19:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by melpomene, Nina Scholz javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 19:22


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • you should remove the space after the g

    – andrea-f
    Nov 21 '18 at 19:15






  • 1





    There is no "outer function".

    – melpomene
    Nov 21 '18 at 19:15






  • 1





    Who in their right mind would even write such a thing...

    – Ben Steward
    Nov 21 '18 at 19:16






  • 1





    maybe you have a look to the comma operator.

    – Nina Scholz
    Nov 21 '18 at 19:19






  • 1





    @NinaScholz That does not fully answer the question. Please read it in full. - "Somehow the second function is the one that is being returned, but what part of the code is actually executing it?"

    – Spencer Wieczorek
    Nov 21 '18 at 19:23





















  • you should remove the space after the g

    – andrea-f
    Nov 21 '18 at 19:15






  • 1





    There is no "outer function".

    – melpomene
    Nov 21 '18 at 19:15






  • 1





    Who in their right mind would even write such a thing...

    – Ben Steward
    Nov 21 '18 at 19:16






  • 1





    maybe you have a look to the comma operator.

    – Nina Scholz
    Nov 21 '18 at 19:19






  • 1





    @NinaScholz That does not fully answer the question. Please read it in full. - "Somehow the second function is the one that is being returned, but what part of the code is actually executing it?"

    – Spencer Wieczorek
    Nov 21 '18 at 19:23



















you should remove the space after the g

– andrea-f
Nov 21 '18 at 19:15





you should remove the space after the g

– andrea-f
Nov 21 '18 at 19:15




1




1





There is no "outer function".

– melpomene
Nov 21 '18 at 19:15





There is no "outer function".

– melpomene
Nov 21 '18 at 19:15




1




1





Who in their right mind would even write such a thing...

– Ben Steward
Nov 21 '18 at 19:16





Who in their right mind would even write such a thing...

– Ben Steward
Nov 21 '18 at 19:16




1




1





maybe you have a look to the comma operator.

– Nina Scholz
Nov 21 '18 at 19:19





maybe you have a look to the comma operator.

– Nina Scholz
Nov 21 '18 at 19:19




1




1





@NinaScholz That does not fully answer the question. Please read it in full. - "Somehow the second function is the one that is being returned, but what part of the code is actually executing it?"

– Spencer Wieczorek
Nov 21 '18 at 19:23







@NinaScholz That does not fully answer the question. Please read it in full. - "Somehow the second function is the one that is being returned, but what part of the code is actually executing it?"

– Spencer Wieczorek
Nov 21 '18 at 19:23














0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)