JavaScript doesn't really have "parameters on the left/right side." Your first example passes a parameter. Your second example uses a closure.
In programming languages, closures (also lexical closures or function closures) are techniques for implementing lexically scoped name binding in languages with first-class functions. Operationally, a closure is a record storing a function together with an environment: a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created. A closure—unlike a plain function—allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.
In your first example, the variable a
inside the function foo
is a different variable than a
outside of the function. Changing foo
's parameter a
has no effect on the global variable a
that gets passed to console.log
.
In your second example, the variable a
is not a parameter, but rather a part of the environment that is captured by the function foo
. The log shows a modified value of a
because the assignment inside foo
and the console.log
call outside of foo
are actually referring to the same variable.