Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript function execution order

Tags:

javascript

I am new to javascript and have a quick question. Say i have the following code:

function entryPoint()
{
   callFunction(parameter);
}

function callFunction(parameter)
{
   ... //do something here
   var anotherFunction = function () { isRun(true); };
}

My question is that when callFunction(parameter) is called, and the variable anotherFunction is declared, does isRun(true) actually execute during this instantiation? I am thinking it doesnt and the contents of the anotherFunction are only "stored" in the variable to be actually executed line by line when, somewhere down the line, the call anotherFunction() is made. Can anyone please clarify the function confusion?

like image 246
John Baum Avatar asked Dec 27 '22 02:12

John Baum


1 Answers

It seems the confusion is this line of code

var anotherFunction = function () { isRun(true); };

This declares a variable of a function / lambda type. The lambda is declared it is not run. The code inside of it will not execute until you invoke it via the variable

anotherFunction(); // Now it runs
like image 78
JaredPar Avatar answered Jan 13 '23 05:01

JaredPar