14

Is it possible to print / display a JavaScript variable's name? For example:

var foo=5;
var bar=6;
var foobar=foo+bar;

document.write(foo+ "<br>");
document.write(bar+ "<br>");
document.write(foobar + "<br>");

How would we print the variable's names so the output would be:

foo 
bar 
foobar

Rather than:

5
6
11
gfrobenius
  • 3,987
  • 8
  • 34
  • 66
Ralph David Abernathy
  • 5,230
  • 11
  • 51
  • 78
  • 2
    This is a typical XY problem. Why do you want to do this? – elclanrs Mar 21 '14 at 02:09
  • 1
    Here's a use case: During testing, I want to check that every variable I think is coming from a module is coming. If `typeof var === undefined`, I'd like to be able to put on the screen "var is undefined". Not an XY question for me. – BaldEagle Oct 11 '16 at 13:05
  • Other posts like this one are marked as duplicates of http://stackoverflow.com/questions/3404057/determine-original-name-of-variable-after-its-passed-to-a-function. Short summary of the discussion in all of them: One could pass the function both a string of the variable name and the variable. For others, there's a way for global variables (those with `window.x`). For others, one could make every variable an object. Then, one can use object keys to get the variable name. For the insane, one could write code to parse the program and get the variable that way (good luck!) For me: That first one. – BaldEagle Oct 11 '16 at 13:28

3 Answers3

4

You can put the variables in an object then easily print them this way: http://jsfiddle.net/5MVde/7/

See fiddle for everything, this is the JavaScript...

var x = {
    foo: 5,
    bar: 6,
    foobar: function (){
        var that=this;
        return that.foo+that.bar
    }
};

var myDiv = document.getElementById("results");

myDiv.innerHTML='Variable Names...';
for(var variable in x)
{
    //alert(variable);
    myDiv.innerHTML+='<br>'+variable;
}

myDiv.innerHTML+='<br><br>And their values...';
myDiv.innerHTML+='<br>'+x.foo+'<br>'+x.bar+'<br>'+x.foobar();

The JavaScript for...in statement loops through the properties of an object.

Another variation (thanks @elclanrs) if you don't want foobar to be a function: http://jsfiddle.net/fQ5hE/2/

gfrobenius
  • 3,987
  • 8
  • 34
  • 66
  • 1
    You forgot `var`, and `this` is `window`. Won't work. – elclanrs Mar 21 '14 at 02:29
  • Your demo shows that the `for..in` works, but the answer is flawed. Try printing `x.foobar` and you'll get `NaN`. – elclanrs Mar 21 '14 at 02:32
  • @elclanrs, thanks. Updated answer. @RalphDavidAbernathy can read this **http://stackoverflow.com/a/134149/3112803** and decide how he wants to handle the `foobar` method. – gfrobenius Mar 21 '14 at 02:51
  • 2
    That looks a bit better, but now it's a function. Do this `var x={foo:5,bar:6}; x.foobar=x.foo+x.bar;`. Also you're still missing a couple vars `var myDiv` and `for(var variable in x)`. Then `myDiv.innerHTML=myDiv.innerHTML` you can write as `myDiv.innerHTML+=` – elclanrs Mar 21 '14 at 02:56
2
Utils = {
    eventRegister_globalVariable : function(variableName,handlers){
        eventRegister_JsonVariable(this,variableName,handlers);
    },
    eventRegister_jsonVariable : function(jsonObj,variableName,handlers){
        if(jsonObj.eventRegisteredVariable === undefined) {
            jsonObj.eventRegisteredVariable={};//this Object is used for trigger event in javascript variable value changes ku
        }
        Object.defineProperty(jsonObj, variableName , {
                    get: function() { 
                        return jsonObj.eventRegisteredVariable[variableName] },
                    set: function(value) {
                        jsonObj.eventRegisteredVariable[variableName] = value; handlers(jsonObj.eventRegisteredVariable[variableName]);}
                    });
            }
Avatar
  • 29
  • 2
0

Another possible solution can be "Object.keys(this)".... This will give you all the variable names in an array.

Mohammad Faraz
  • 544
  • 4
  • 7