Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *