Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5945343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:40:05+00:00 2026-05-22T16:40:05+00:00

How can I refer to the variable’s name (literally the name of the variable)?

  • 0

How can I refer to the variable’s name (literally the name of the variable)?

I have the following code:

var thisVariableName = 'path/filename.js';

var script = document.createElement('script');
script.id = **I WANT THE TEXT "thisVariableName" to be added HERE**
script.type = 'text/javascript';
script.src = thisVariableName;

So that if output script.id, I would get thisVariableName as such, not the value of the variable

AFTER SOME OF THE RESPONSES:

I want to systematize the addition of many script files.
In the example below I add four files, but imagine I had to add 30+ files (please reserve yourself from asking why would I need to add 30+ files; after all this is the promise of computing)

As of now, the only way I know how to get this done is to create two separate arrays; array 1 for the files’ name string, and array 2 for the files’ paths, or ALTERNATIVELLY, as Felix Kling SUGGESTS create an object per each variable:

var jQueryPath = 'js/jquery-1.5.1.js';
var jQueryUICore = 'js/jquery.ui.core.js';
var jQueryUIWidget = 'js/jquery.ui.widget.js';
var jQueryUITabs = 'js/jquery.ui.tabs.js';

var fileNames = new Array ('jQueryPath','jQueryUICore','jQueryUIWidget','jQueryUITabs');

var filePaths = new Array (jQueryPath,jQueryUICore,jQueryUIWidget,jQueryUITabs);

var head = document.getElementsByTagName("head")[0]; 

for (var i=0;i<4;i++){

    var script = document.createElement('script');
    script.setAttribute('id',fileNames[i]);
    script.setAttribute('type','text/javascript');
    script.setAttribute('src', filePaths[i]);
    head.appendChild(script);

    }

So whether I create arrays to hold the names and the values of the variables, or create an object, it requires individual creation of both attributes per file.

I was hoping I could reference the literal variables’ names of an array composed of the variables; i.e:

var onlyOneArray = new Array (jQueryPath,jQueryUICore,jQueryUIWidget,jQueryUITabs);

for (var i=0;i<4;i++){

        var script = document.createElement('script');
        script.setAttribute('id',**onlyOneArra[i] the NAME**);
        script.setAttribute('type','text/javascript');
        script.setAttribute('src', onlyOneArray[i]);
        head.appendChild(script);

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:40:06+00:00Added an answer on May 22, 2026 at 4:40 pm

    You cannot get the name of a variable at run time.

    I suggest to follow a more organized approach, e.g. by storing the variable as property of an object. You can then iterate over this object:

    var scripts = {
        thisVariableName: 'path/filename.js'
    };
    
    for(var name in scripts) {
        if(scripts.hasOwnProperty(name)) {
            var script = document.createElement('script');
            script.id = name;
            script.type = 'text/javascript';
            script.src = scripts[name];
            //....
        }
    }
    

    Update: Regarding your changes, what you should end up doing, is :

    var scripts = {
        jQueryPath: 'js/jquery-1.5.1.js',
        jQueryUICore: 'js/jquery.ui.core.js',
        jQueryUIWidget: 'js/jquery.ui.widget.js',
        jQueryUITabs: 'js/jquery.ui.tabs.js'
    };
    
    var head = document.getElementsByTagName("head")[0];
    
    for(var name in scripts) {
        if(scripts.hasOwnProperty(name)) {
            var script = document.createElement('script');
            script.setAttribute('id', name);
            script.setAttribute('type', 'text/javascript');
            script.setAttribute('src', scripts[name]);
            head.appendChild(script);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way wherein an xml document can refer to a variable/element which
Code inside closures can refer to it variable. 8.times { println it } or
I am aware that I can't dynamically refer to a variable (eg. 'var &
If I have var doc=window.document; and I don't want to define another variable, how
In my app, I have a facebook field that can refer to a facebook
Consider the following code. <!DOCTYPE html> <script> console.log(a); function a() {} </script> Notice that
In ggplot2 , how do I refer to a variable name with spaces? Why
Can someone tell me why this is not working? Throughout my document I have
How can I make it so when I refer to the display1 variable, I
I know I can refer in replacement to dynamic parts of the term in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.