Get global variable dynamically by name string in JavaScript?

In JavaScript, you can access global variables dynamically using their name as a string through the window object (in browsers) or global object (in Node.js).

Syntax

// Browser environment
window[variableName]

// Node.js environment  
global[variableName]

// Using globalThis (works in both)
globalThis[variableName]

Basic Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dynamic Global Variables</title>
</head>
<body>
    <script>
        // Define global variables
        var userName = "John";
        var userAge = 25;
        var userCity = "New York";
        
        // Access using string names
        console.log(window["userName"]);   // John
        console.log(window["userAge"]);    // 25
        console.log(window["userCity"]);   // New York
        
        // Dynamic access with concatenated strings
        var prefix = "user";
        console.log(window[prefix + "Name"]);  // John
        console.log(window[prefix + "Age"]);   // 25
    </script>
</body>
</html>
John
25
New York
John
25

Dynamic Variable Construction

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dynamic Variable Names</title>
</head>
<body>
    <script>
        // Global variables with pattern
        var globalVariablelocal_print100 = 100;
        var globalVariablelocal_print200 = 200;
        var print100 = 1;
        var print200 = 2;
        
        // Build variable name dynamically
        var baseName = "globalVariable";
        var middlePart = "local_print";
        var suffix = "00";
        
        var dynamicVarName1 = baseName + "local_print1" + suffix;
        var dynamicVarName2 = baseName + "local_print2" + suffix;
        
        console.log("First variable:", window[dynamicVarName1]);   // 100
        console.log("Second variable:", window[dynamicVarName2]);  // 200
        
        // Using concatenation like in original example
        alert(window['globalVariable' + 'local_print1' + '00']);
    </script>
</body>
</html>
First variable: 100
Second variable: 200

Using globalThis (Modern Approach)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>globalThis Example</title>
</head>
<body>
    <script>
        // Global variables
        var config_apiUrl = "https://api.example.com";
        var config_timeout = 5000;
        
        // Function to get config values dynamically
        function getConfig(key) {
            var varName = "config_" + key;
            return globalThis[varName];
        }
        
        console.log("API URL:", getConfig("apiUrl"));    // https://api.example.com
        console.log("Timeout:", getConfig("timeout"));   // 5000
        console.log("Missing:", getConfig("missing"));   // undefined
    </script>
</body>
</html>
API URL: https://api.example.com
Timeout: 5000
Missing: undefined

Key Points

  • window[variableName] works in browsers
  • global[variableName] works in Node.js
  • globalThis[variableName] works in both environments
  • Returns undefined if the variable doesn't exist
  • Only works with variables declared with var or without declaration keywords
  • Variables declared with let or const are not added to the global object

Conclusion

Use window[variableName] in browsers or globalThis[variableName] for cross-platform compatibility to access global variables dynamically by their string names.

Updated on: 2026-03-15T23:18:59+05:30

531 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements