To create a function in Java Script you need a key word called “function” , a name with parentheses to hold or not hold parameters. And finally within brackets a set of code or instruction that need to be operated when the function is called.
The difference between a var and a let statement is that a var is a global binding and a let is a local binding that can only be seen by the block or function that contains it. A global binding can be seen by the entire program.
Take this example script and run this code to get a very clear understanding of how the java Script engine works in defining holding and executing variables that are global vs. local.
function c() {console.log(myVar)}
function b() {var myVar = 3; console.log(myVar);c()}
function a() {var myVar = 2; console.log(myVar);b();}
var myVar = 1; console.log(myVar);
a(); console.log(myVar)
The Java Script is run using a Global Execution stack synchronously step by step. The Java engine will load values and function names but not function attributes until they are called but for the “var” designation that is outside of the function it will load values for these variables in memory.
So if we go step by step we see that the initial value of Myvar = 1 And this is a global binding meaning it will be seen by the entire program.
No lets see what happen when we do successive "console.log (myVar) to see the value of myVar at each stage the code is being executed. The first three lines of code designate functions c(), b(), and a() but these function are not executed until the function is called. Line four is the global binding of
1.) myVar = 1 ( global binding for myVar set in computer memory not printed but held
in execution stack memory until console.log(myVar ) executed. and
myVar = 1
2.) myVar = 2 ( next the function a() is called and put in the Execution stack with its
own Execution context of variables. var myVar =2 for this function a()
and console.log prints myVar = 2. Next within fucntion a() the function
b() is called and executed
3 myVar = 3 (next step in code going synchronously within the function a() is a call
for function(b). This puts function b() at the top of the execution stack
with its own set of variables and Execution context. When b() is
executed var myVar = 3 and console.log(myVar) will print myVar = 3.
The function b() will then call function c() which will now be put on top of
the execution stack with its own set of variables and execution
context.
4.) myVar = 1 (the function c() is called in the next step moving synchronously the
engine will look for the function a() and uses parameters if available
to execute the operation of the function which states
console.log(myVar) but does not define myVar and thus the engine
does not have any variable loaded for function c() and will then look
globally for value of “myVar” and will find myVar = 1 as global binding.
4.) myVar = 1 The next step within the code console.log(myVar)
and the Java Engine jumps out of the execution of function c() and
back into its global environment where myVar = 1 each time
console.log is executed.
A pure function is one with no side effects. Unique input gives the same output each time.