Change the Value of a Global Variable Inside a Function in JavaScript

Last Updated : 22 Aug, 2026

Global variables can be accessed and modified from inside functions. To change a global variable, declare it outside the function and assign a new value to it inside the function.

  • Declare the variable outside the function to make it globally accessible.
  • Access the global variable directly inside the function.
  • Assigning a new value inside the function updates the global variable.
  • Avoid creating undeclared global variables; use let, const, or var explicitly.

Changing a Global Variable Inside a Function

A global variable declared outside a function can be modified directly inside that function.

JavaScript
let count = 10;

function updateCount() {
    count = 20;
}

updateCount();

console.log(count);

Output
20
  • count is declared outside the function, so it is accessible inside updateCount().
  • The statement count = 20 changes the existing global variable.
  • After calling updateCount(), the value of count becomes 20.

Example: Updating Global Variables Using User Input

The following example takes two numbers from input fields and updates global variables inside the add() and subtract() functions.

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
        let firstNum = 0;
        let secondNum = 0;

        function add() {
            // Update global variables
            firstNum = Number(document.getElementById("fNum").value);
            secondNum = Number(document.getElementById("sNum").value);

            const result = firstNum + secondNum;

            document.getElementById("result").textContent =
                "Sum of 2 numbers is " + result;
        }

        function subtract() {
            // Update global variables
            firstNum = Number(document.getElementById("fNum").value);
            secondNum = Number(document.getElementById("sNum").value);

            const result = firstNum - secondNum;

            document.getElementById("result").textContent =
                "Difference of 2 numbers is " + result;
        }
    </script>
</body>
  • firstNum and secondNum are declared outside both functions, making them accessible to add() and subtract().
  • Each function assigns new values to these global variables.
  • The updated values are then used to perform the required calculation.
  • const result is local to each function because it is only needed for that calculation.

Note: Avoid assigning a value to a variable without declaring it, such as count = 10 in non-strict code. This can create an unintended global variable. Prefer explicitly declaring variables with let, const, or var.

Also Check:

Comment