Matt Sadewa February 2016

Are Variables Initialized inside Function Declaration Always Re-initialized When The Function Execute (Javascript)?

My question is based on two premises: (1) codes are not executed until the function containing them is called and (2) variables are saved in memory.

So, are variables initialized inside function declaration always re-initialized when the function execute?

To demonstrate, if I initialize a variable inside function declaration to a huge object and call that function, then, the huge object will be created, saved, or and processed in memory. If I call or execute that function many times, then the huge object will be created, saved, or and processed everytime the function executes. Then, there will be so many and big processing of that variable (containing that huge object, and if only one). Therefore, this behavior will result in bad effect for performance. I do not know much about this, is this correct?

This is to demonstrate with code: If I declare global variables, then the process only involve value-changing. But, if I declare local variable like this:

var hugeObj = {
   prop1 : function() {...},
   prop2 : [...],
   prop3 : {...},
};

and I execute the containing function five times, then there will be five "hugeObj" (with different contexts) in memory (and involve more processing).

Can you explain how variables are processed inside function declarations, and will them be created in every execution context?

Answers


Dustin Poissant February 2016

The answer is yes, each time the function is run it's internal variables are re-initialized and are not reused.

function MyFunc(newValue){
  var v;
  if(newValue) v = newValue;
  return v;
};


console.log(MyFunc()); // undefined
console.log(MyFunc(5)); // 5
console.log(MyFunc()); // undefined

// Therefore we can conclude that the object is re-initialized each time the function is run and its previous variable values are not reused.

If you would like it to not initialize the variable each time, just initialize the variable globally.

var v;
function MyFunc(newValue){
  if(newValue) v = newValue;
  return v;
};


console.log(MyFunc()); // undefined
console.log(MyFunc(5)); // 5
console.log(MyFunc()); // 5


Babajide Apata February 2016

Variables are named memory, it all depend on the scope of the variable(Local or Global) Variables inside a functions are called local variables, every time this function is called the variables are re initialized, which means at every call to the function the value of the variable is expected to change.

public void myFunction(int val1, String val2)
{
 int a = val1;
 String b = val2;
 System.out.println(val2 +" "+val1);
}

a and b in the above are local variable that will be affected base on the parameters passed to it by the method/function calls so the value of a variables in a function will always change


Marcos Pérez Gude February 2016

In addition to the answer of @DustinPoissant, you can declare different scope (local or global) inside a function.

Let's use the same code as the answer:

function MyFunc(newValue){
  var v;
  if(newValue) v = newValue;
  return v;
};


console.log(MyFunc()); // undefined
console.log(MyFunc(5)); // 5
console.log(MyFunc()); // undefined

In this case the scope is the local function, but if you remvoe var keyword, your scope changes to global (yes, v variable will be attached to window object):

function MyFunc(newValue){
  if(newValue) v = newValue;
  return v;
};


console.log(MyFunc()); // undefined
console.log(MyFunc(5)); // 5
console.log(MyFunc()); // 5

That's the same as write:

function MyFunc(newValue){
  if(newValue) window.v = newValue;
  return window.v;
};


console.log(MyFunc()); // undefined
console.log(MyFunc(5)); // 5
console.log(MyFunc()); // 5


Jordan Lowe February 2016

Because one of the variables is stored externally, run both, one will maintain its values, the one with the variable initialised within the function is overwritten.

var array = ["1","2","3","4","5"];

function MyFunc()
{
  var current = "0";
  console.log("Iteration");
  for(var i = 0; i < array.length; i++)
  {
    current += array[i];
    console.log(current);
  }
};

console.log(MyFunc());
console.log(MyFunc());

var array = ["1","2","3","4","5"];
var current = "0";

function MyFunc()
{
  console.log("Iteration");
  for(var i = 0; i < array.length; i++)
  {
    current += array[i];
    console.log(current);
  }
};

console.log(MyFunc());
console.log(MyFunc());

Post Status

Asked in February 2016
Viewed 2,592 times
Voted 13
Answered 4 times

Search




Leave an answer