How to change the value of a global variable inside of a function using JavaScript ? Last Updated : 13 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Pre-requisite: Global and Local variables in JavaScript Local Scope: Variables that are declared inside a function is called local variables and these are accessed only inside the function. Local variables are deleted when the function is completed. Global Scope: Global variables can be accessed from inside and outside the function. They are deleted when the browser window is closed but are available to other pages loaded on the same window. There are two ways to declare a variable globally: Declare a variable outside the functions.Assign a value to a variable inside a function without declaring it using the "var" keyword. Example: In this example, we will define global variables with user input and manipulate them from inside the function HTML <body> <h1 style="color:green"> GeeksForGeeks </h1> <b>Enter first number :- </b> <input type="number" id="fNum"> <br><br> <b>Enter second number :- </b> <input type="number" id="sNum"> <br><br> <button onclick="add()">Add</button> <button onclick="subtract()">Subtract</button> <p id="result" style="color:green; font-weight:bold;"> </p> <script> // Declare global variables var globalFirstNum1 = 9; var globalSecondNum1 = 8; function add() { // Access and change globalFirstNum1 and globalSecondNum1 globalFirstNum1 = Number(document.getElementById("fNum").value); globalSecondNum1 = Number(document.getElementById("sNum").value); // Add local variables var result = globalFirstNum1 + globalSecondNum1; var output = "Sum of 2 numbers is " + result; // Display result document.getElementById("result").innerHTML = output; } // Declare global variables globalFirstNum2 = 8; globalSecondNum2 = 9; function subtract() { // Access and change globalFirstNum2 // and globalSecondNum2 globalFirstNum2 = Number(document.getElementById("fNum").value); globalSecondNum2 = Number(document.getElementById("sNum").value); // Use global variables to subtract numbers var result = globalFirstNum2 - globalSecondNum2; var output = "Difference of 2 numbers is " + result; document.getElementById("result").innerHTML = output; } </script> </body> Output: Comment More infoAdvertise with us Next Article How to change the value of a global variable inside of a function using JavaScript ? A AshaRamMeena Follow Improve Article Tags : JavaScript Web Technologies javascript-functions JavaScript-Questions Similar Reads How to declare the optional function parameters in JavaScript ? Declaring optional function parameters in JavaScript means defining function parameters that aren't required when the function is called. You can assign default values to these parameters using the = syntax, so if no argument is provided, the default value is used instead. These are the following ap 3 min read How to get the javascript function parameter names/values dynamically ? In this article, we are given any arbitrary JavaScript function and the task is to return the parameter names of the function. Approach: JavaScript contains a method called toString() which is used to represent a function code in its string representation. This method is used to get the parameter na 2 min read How To Include a JavaScript File in Another JavaScript File? The import and export syntax available in ES6 (ECMAScript 2015) module is used to include a JavaScript file in another JavaScript file. This approach is the most modern and recommended way to share code between JavaScript files.It allows you to break your code into smaller modules and then import th 4 min read How to override a JavaScript function ? In this article, we are given an HTML document and the task is to override the function, either a predefined function or a user-defined function using JavaScript. Approach: When we run the script then Fun() function is called. After clicking the button the GFG_Fun() function is called and this funct 2 min read Using the function* Declaration in JavaScript The function* declaration is used to define a generator that returns a Generator object. Generators are very powerful for asynchronous programming as they aim to resolve callback problems. In a generator, the yield keyword is used instead of return. The yield statement suspends the functionâs execut 2 min read What is the difference between call and apply in JavaScript ? JavaScript call() Method: It calls the method, taking the owner object as an argument. The keyword this refers to the 'owner' of the function or the object it belongs to. We can call a method that can be used on different objects. Syntax: object.objectMethod.call( objectInstance, arguments ) JavaScr 2 min read How to find out the caller function in JavaScript? In this article, we see the methods to find out the caller function in Javascript. Sometimes, the developer may want to modify how a function works on the basis of its caller function. To find out the caller function name, we will use the Function object's caller property. Property: Function.caller 1 min read How to negate a predicate function in JavaScript ? In this article, we will see how to negate a predicate function in JavaScript. Predicate functions are the ones that check the condition and return true and false based on the argument. Our task is to get the opposite of the predicate function. We follow the following method to get the desired resul 3 min read How to iterate over a callback n times in JavaScript? Given a callback function, we have to iterate over a callback n times. The callback is a function that is passed as an argument. To iterate over the callback function, we have to run the callback function n time.Here we have some common approaches:Table of ContentUsing recursion to iterate the n tim 4 min read How to call function from it name stored in a string using JavaScript ? In this article, we will call a function from the string stored in a variable. There are two methods to call a function from a string stored in a variable. Using window object methodUsing eval() method Note: The eval() method is older and is deprecated. Method 1: Using the window object. The window 2 min read Like