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/
typeof var === undefined
, I'd like to be able to put on the screen "var is undefined". Not an XY question for me. – Clothowindow.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. – Clotho