Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function returned from another function

I observed a difference in output when trying to run similar function.

function func1(){
     function sum(){
        console.log(2+5);
    }
    return sum();
}
func1();

function func2(){
    return (function sum(){
       console.log(2+5);
   })
    
}

func2();

From func1, the output is 7. But from func2, nothing is there in the console. What is the difference in both functions as both are returned from the function? Why did one get invoked and another didn’t?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From func1, the output is 7. But from func2, nothing is there in the console. What is the difference in both functions as both are returned from the function?
Answer the difference between both is, the first one’s return statement calls the the sum function.

In the second one the function’s definition is returned hence it’s defined but not called resulting into no output.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading