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
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...
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/Another possible solution can be "Object.keys(this)".... This will give you all the variable names in an array.