Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print / display a JavaScript variable's name instead of it's value

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
like image 516
Ralph David Abernathy Avatar asked Mar 21 '14 02:03

Ralph David Abernathy


People also ask

Can you print variable name?

To print a variable's name: Use a formatted string literal to get the variable's name and value. Split the string on the equal sign and get the variable's name. Use the print() function to print the variable's name.

How do you print variable names in go?

🤔 Print type of variable in Go To print a variable's type, you can use the %T verb in the fmt. Printf() function format. It's the simplest and most recommended way of printing type of a variable. Alternatively, you can use the TypeOf() function from the reflection package reflect .

Does JavaScript have a print statement?

JavaScript PrintJavaScript does not have any print object or print methods. You cannot access output devices from JavaScript. The only exception is that you can call the window.print() method in the browser to print the content of the current window.


2 Answers

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]);}
                    });
            }
like image 82
Avatar Avatar answered Sep 20 '22 13:09

Avatar


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/

like image 39
gfrobenius Avatar answered Sep 19 '22 13:09

gfrobenius