-1

I have a function that takes 2 inputs, the variable name as a string and the variable itself - it prints both via console.

var variable1 = ["entry1","entry2"];
   function logthis(input1, input2){
    console.log(input1+ "\n " + input2)
    }
    
    logthis("variable1", variable1) ;

I want to be able to use it as logthis(variable1); and get the same result.

How can I reference the variable name (as a string) and not its contents?

eg, console.log(input2.name) will not work

Something like the C+ equivalent of "nameOf(variable1)"

platinums
  • 634
  • 1
  • 10
  • 21

2 Answers2

1

var variable1 = ["entry1","entry2"];
function logthis(input1, input2){
  console.log(input1+ "\n " + input2)
}
    
logthis('variable1', variable1) ;

You are sending the reference. Pass first argument as string.

Omaim
  • 234
  • 3
  • 9
0

Try to get your input from the table first

function logthis(inputTable){
       var input1 = inputTable[0];
       var input2 = inputTable[1];

       console.log(input1+ "\n " + input2) // will display "entry1 \n entry2 "
    }
        
    var table1 = ["entry1","entry2"];
    logthis(table1);
platinums
  • 634
  • 1
  • 10
  • 21
andrea06590
  • 1,259
  • 3
  • 10
  • 22