-3

For example:

var apple = ["red", "green"];

I'd like to it to either return apple or print out apple (as the name of the array and not as a string) and also not print or return the array that apple refers to.

Does this have anything to do with prototype?

Lina
  • 315
  • 3
  • 12
  • I can see a work around, but it depends on what you're trying to do with the string? Are you trying to compare it to something? –  May 27 '15 at 02:54
  • 2
    This may be a duplicate of the question asked at http://stackoverflow.com/questions/22548885/print-display-a-javascript-variables-name-instead-of-its-value – gjanden May 27 '15 at 02:55
  • 1
    the short answer is you can't do that as-is. You need to restructure what you are doing (e.g. other SO link posted / answer below) – CrayonViolent May 27 '15 at 03:03
  • 1
    like @CrayonViolent said, this is probably an XY-problem. Instead of trying to do this, rethink your approach. They are, after all, called variables. You care about the content, not the variable name. If you need to record a name of something, use another variable. (Plus, you should not write code that is dependent on variable names—that sort of defeats the purpose of programming.) – royhowie May 27 '15 at 03:07
  • I do not understand the question.. `print out apple as the name of array and not as a string`.. what does that mean.. `How do I return the variable name instead of the array or object it's holding?` ... you do not want an object, and you do not want a String too... If you are gonna print a name .. it is always going to be a string .. – Anurag Anand May 27 '15 at 03:14

1 Answers1

2

Put the variable in an object, this will allow you to access both the name and the value(s)

var variables = {
    apple: ["red", "green"]
}
for(key in variables)
    console.log(key)
Mike
  • 555
  • 3
  • 14