12

Possible Duplicate:
Determine original name of variable after its passed to a function.

I would like to know if its possible to get the actual name of a variable.

For example:

var foo = 'bar';
function getName(myvar) {  
  //some code
  return "foo"  
};  

So for getName(foo) will return "foo"

Is that possible ?

Thanks.

Community
  • 1
  • 1
shdev
  • 1,855
  • 4
  • 17
  • 18
  • 2
    why would you be interested in variable name? Rather your logic in functions should depend on variable values right? – Sachin Shanbhag Aug 25 '10 at 10:47
  • I find this code extremely weird. I wonder what you're trying to accomplish? – axel_c Aug 25 '10 at 10:50
  • Luckily this isn't possible in JavaScript, otherwise you'd end up with something horrible³ like this... http://stackoverflow.com/questions/2749796/how-to-get-the-original-variable-name-of-variable-passed-to-a-function/2749857#2749857 – Ivo Wetzel Aug 25 '10 at 10:56
  • Never seen it done, why would you need it anyway? You already know it. It sounds like you should restructure your scripts to remove this neccessity. – Tom Gullen Aug 25 '10 at 10:50
  • Also a duplicate of the 2009 [How to convert variable name to string in JavaScript?](http://stackoverflow.com/questions/417645/how-to-convert-variable-name-to-string-in-javascript) – Dan Dascalescu Mar 10 '14 at 13:57
  • 1
    This is pretty useful feature if you trying build a debugger or auto register a namespace without redeclaring or declaring another variable. I see some extremely useful cases for this. – flyandi Apr 15 '15 at 21:02
  • See `nameof` operator in C# for use case. [msdn](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/nameof) – aloisdg Jan 30 '19 at 10:06

1 Answers1

8

I don't think it is possible. When you call a function you pass an object, not a variable. The function doesn't care where the object came from.

You can go the other way though if you call your function as follows:

getName('foo') 

Or pass both the value and the name:

getName(foo, 'foo') 
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 2
    Just a comment, why would I pass a harcoded string value 'foo' when I know I am going to get it as return value? – Sachin Shanbhag Aug 25 '10 at 11:25
  • @Sachin Shanbhag: How exactly will you get it as a return value if you don't pass it in as a parameter? You're assuming that what the OP is asking is possible. Perhaps it is... but I'd *really* like to see some evidence of that please. – Mark Byers Aug 25 '10 at 11:27
  • I totally agree with you. This is one of the possible ways to getting what is required, but from programming perspective, if I am using that return value for further operations, I already know that value before passing it to function and need not wait for the return value. No offence, but I am actually assuming getting variable name is not posisble since it does not help in programming. – Sachin Shanbhag Aug 25 '10 at 11:34
  • 1
    I know it's way old, but for future reference... It does help in programming, in some very speficic features, it's actually possible in c# using the nameof operator (wrong results if used for arguments as the OP wanted, but I believe he wouldn't need a function). I came to this exactly with a need. Think about it when you have a process to change other process parameters and you want the other process to react to some specific parameters changes. Would be very fragile to do something like `if (argumentName == 'myArgument')` because the name could change. Rather `== nameof(myArgument)` – Cleivson Arruda Aug 14 '17 at 12:31
  • 1
    This could be useful for refactoring during debugging.. – tBlabs Oct 02 '17 at 08:27
  • 3
    You can write function `getName(variable){ return Objects.keys(variable)[0]}` and call it this way `var abc = "xyz"; console.log(getName({abc}));` – itsmnthn Oct 09 '20 at 13:27