Change global variable in function javascript

I am using JavaScript and I create a global variable. I define it outside of a function and I want to change the global variable value from inside a function and use it from another function, how do I do this?

Necreaux

9,1727 gold badges25 silver badges43 bronze badges

asked Jun 3, 2012 at 16:30

NullPoiиteяNullPoiиteя

55.5k22 gold badges123 silver badges140 bronze badges

0

Just reference the variable inside the function; no magic, just use it's name. If it's been created globally, then you'll be updating the global variable.

You can override this behaviour by declaring it locally using var, but if you don't use var, then a variable name used in a function will be global if that variable has been declared globally.

That's why it's considered best practice to always declare your variables explicitly with var. Because if you forget it, you can start messing with globals by accident. It's an easy mistake to make. But in your case, this turn around and becomes an easy answer to your question.

answered Jun 3, 2012 at 22:41

6

var a = 10;

myFunction[];

function myFunction[]{
   a = 20;
}

alert["Value of 'a' outside the function " + a]; //outputs 20

NullPoiиteя

55.5k22 gold badges123 silver badges140 bronze badges

answered Jun 3, 2012 at 16:34

ChrisChris

4,1996 gold badges40 silver badges83 bronze badges

3

Just use the name of that variable.

In JavaScript, variables are only local to a function, if they are the function's parameter[s] or if you declare them as local explicitely by typing the var keyword before the name of the variable.

If the name of the local value has the same name as the global value, use the window object

See this jsfiddle

x = 1;
y = 2;
z = 3;

function a[y] {
  // y is local to the function, because it is a function parameter
  console.log['local y: should be 10:', y]; // local y through function parameter
  y = 3; // will only overwrite local y, not 'global' y
  console.log['local y: should be 3:', y]; // local y
  // global value could be accessed by referencing through window object
  console.log['global y: should be 2:', window.y] // global y, different from local y []

  var x; // makes x a local variable
  x = 4; // only overwrites local x
  console.log['local x: should be 4:', x]; // local x
  
  z = 5; // overwrites global z, because there is no local z
  console.log['local z: should be 5:', z]; // local z, same as global
  console.log['global z: should be 5 5:', window.z, z] // global z, same as z, because z is not local
}
a[10];
console.log['global x: should be 1:', x]; // global x
console.log['global y: should be 2:', y]; // global y
console.log['global z: should be 5:', z]; // global z, overwritten in function a

Edit

With ES2015 there came two more keywords const and let, which also affect the scope of a variable [Language Specification]

answered Jun 3, 2012 at 16:35

yunzenyunzen

32k11 gold badges71 silver badges100 bronze badges

2

var a = 10;

myFunction[a];

function myFunction[a]{
   window['a'] = 20; // or window.a
}

alert["Value of 'a' outside the function " + a]; //outputs 20

With window['variableName'] or window.variableName you can modify the value of a global variable inside a function.

answered Mar 28, 2018 at 3:54

Sterling DiazSterling Diaz

3,5732 gold badges29 silver badges35 bronze badges

1


var x = 2; //X is global and value is 2.

function myFunction[]
{
 x = 7; //x is local variable and value is 7.

}

myFunction[];

alert[x]; //x is gobal variable and the value is 7

answered Mar 10, 2014 at 8:22

Iman SedighiIman Sedighi

7,0664 gold badges48 silver badges53 bronze badges

1

A simple way is to use var

var apple = null;
const some_func =[]=>{
      apple = 25
}
some_func[]
console.log[apple]

answered Mar 27, 2021 at 23:52

1

Not the answer you're looking for? Browse other questions tagged javascript or ask your own question.

Can a function change a global variable JS?

Functions can access global variables and modify them. Modifying global variables in a function is considered poor programming practice.

How do you update a global variable in a function?

If your function has a local variable with same name as global variable and you want to modify the global variable inside function then use 'global' keyword before the variable name at start of function i.e. As you can see modification done to global variable total is now visible outside the function too.

How do you update a variable in JavaScript?

Updating Variables We can update our variables in shorter ways by using the += , *= , -= or /= operators. This way, we don't have to repeat the variable name when we assign them to something. Then we increase the value of i by 2. If we increment or decrement only by 1, then we can use ++ or -- respectively.

Are variables in JavaScript functions global?

variable in javascript? The scope of JavaScript variables are either global or local. Global variables are declared OUTSIDE the function and its value is accessible/changeable throughout the program.

Chủ Đề