How to change the value of a global variable inside of a function with JavaScript?

Sometimes, we want to change the value of a global variable inside of a function with JavaScript.

In this article, we’ll look at how to change the value of a global variable inside of a function with JavaScript.

How to change the value of a global variable inside of a function with JavaScript?

To change the value of a global variable inside of a function with JavaScript, we can assign the global variable to a new value.

For instance, we write

a = 10;

const myFunction = () => {
  a = 20;
};

myFunction();

to set the global variable a to 20 in the myFunction function.

Conclusion

To change the value of a global variable inside of a function with JavaScript, we can assign the global variable to a new value.