जावास्क्रिप्ट में, आप window ऑब्जेक्ट का उपयोग करके स्ट्रिंग नाम से ग्लोबल वेरिएबल को डायनामिकली एक्सेस कर सकते हैं। सिंटैक्स इस प्रकार है:
alert(window['yourVariableName' + 'yourVariableName' + otherVariableName]);
उदाहरण
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <script> var globalVariablelocal_print100=100; print100=1; alert(window['globalVariable' + 'local_' + print100]); </script> </body> </html>
उपरोक्त प्रोग्राम को चलाने के लिए, फ़ाइल को anyName.html (index.html) नाम से सेव करें और VS कोड एडिटर में फ़ाइल पर राइट-क्लिक करके "Open with Live Server" विकल्प चुनें।
आउटपुट
