-2

I am trying to figure out how to get a variable's name from code in javascript.

Ex:

var MYVAR = 3;//this is in file a.txt
abc(MYVAR);//this is in file a.txt

function abc(label) {//this is in file b.txt
    alert(label); // this will print 3, but i want to print the name of the variable i.e., MYVAR
}

Required OUTPUT: "MYVAR"

File a.txt cannot be changed, only file b.txt can be changed

  • anyone know this?.. is it possible in the current spec of the language???
Shreelakshmi G
  • 119
  • 2
  • 16
  • 2
    Quite impossible. – CertainPerformance Jul 12 '18 at 09:58
  • 4
    why do you need that? – B001ᛦ Jul 12 '18 at 09:59
  • Checkout this => https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript – David R Jul 12 '18 at 10:00
  • window[] is throwing error. It will not work – Shreelakshmi G Jul 12 '18 at 10:05
  • No, it is not possible in any sane way. Why do you need this? The only sane reason to ever need this is for debugging, and there you're better off using a proper debugger, setting a breakpoint, and inspecting the stack trace. – deceze Jul 12 '18 at 10:22
  • Its not for debugging, Its required to add another customised feature to customer – Shreelakshmi G Jul 12 '18 at 10:25
  • 1
    Variable names are nothing you should base business logic on. Variable names can change and be refactored as the local code requires. Variables are placeholders in an algorithm, they do not carry business logic meaning. Variable names may be mangled when compiled and/or minified. **Do not try to depend on variables names in external code.** – deceze Jul 12 '18 at 10:28

1 Answers1

0

Here is a DIY solution for your problem.

You can cast your variable into an object and then use Object.keys to get the variable name in a string format.


var MYVAR = 3;
abc({MYVAR});

function abc(label) {
  if (typeof label === 'object') {
    alert(Object.keys(label)[0]); // Alerts variable name
  } else {
    alert(label); // Alerts variable value
  }
}
Alex
  • 2,164
  • 1
  • 9
  • 27
  • Actually var MYVAR = 3; abc(MYVAR); These are in 1 file. Below code is in another file function abc(label) { alert(label); // this will print 3, but i want to print the name of the variable i.e., MYVAR } I can change only this code. But not the 1st file code – Shreelakshmi G Jul 12 '18 at 10:06
  • What do you mean OP? @ShreelakshmiG – Alex Jul 12 '18 at 10:07
  • OP?? I didn't get U @Alex – Shreelakshmi G Jul 12 '18 at 10:11
  • 1
    _OP??_ OP = Oiginal Poster.. in that case it is you! @ShreelakshmiG – B001ᛦ Jul 12 '18 at 10:13
  • @ShreelakshmiG this is the closest you can get to. You will still have to wrap your variable in curly braces `{}` when you're passing the variable into your function. If you don't wrap it, it will output the value instead. – Alex Jul 12 '18 at 10:16
  • As you can see the question I have explained just in a more clear way. Both are in different files and the file where we call the function is not editable. We can make changes in the function definition only. – Shreelakshmi G Jul 12 '18 at 10:22
  • That's pretty much the most you can do. If you don't have control over the first file, then you can't use this function. Can you edit the code where you're invokin `abc()` ? – Alex Jul 12 '18 at 10:25
  • No, I can't edit the code of invoking the function abc(). Is it possible to convert the variable MYVAR to object in function definition so that we can get the name and value?? – Shreelakshmi G Jul 12 '18 at 10:37
  • It will not work as the variable you pass into your function is passed by reference, meaning that you can only access the value. – Alex Jul 12 '18 at 10:39
  • Oh. Okay. Thanks for the quick response – Shreelakshmi G Jul 12 '18 at 10:43
  • @ShreelakshmiG no problem! :) Let's hope you find something. – Alex Jul 12 '18 at 10:48