Not the value of a variable, but its name.

  • use an object and its key-> value var obj = { myFirstName: 'John' }; obj.foo = 'Another name'; for(key in obj) alert(key + ': ' + obj[key]); var obj = { myFirstName: 'John' }; obj.foo = 'Another name'; for(key in obj) alert(key + ': ' + obj[key]); - Alexey Shimansky

4 answers 4

Hmm, yes nothing.
You can, however, make such a trick:

 (function() { var getVarName = function tmp(){ let n = /getVarName\(([^)]+?)\)/.exec(tmp.caller !== null ? tmp.caller.toString() : ''); return n !== null ? n[1] : false; } let myVarName123456 = 1; console.info(getVarName(myVarName123456)); // myVarName123456 }()); 

But this is a dirty hack, uncomfortable, and more than just a β€œmake to dispute” than a worker; in real code, I don't even know who could use it.


Perhaps you should reconsider the architecture of your project, where the variable identifier is used as a dependent variable.
Variables just come up with not to depend on the names.

    To get the name, you must wrap the variable with an object, like this:

     <script> var myVariable = {}; myVariable["myVariable"] = 5; // Π·Π°Π²Π°Ρ€Π°Ρ‡ΠΈΠ²Π°Π΅ΠΌ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ Π² массив myVariable["myVariable"] = 7; // Π²ΠΎΡ‚ Ρ‚Π°ΠΊ Ρ€Π°Π±ΠΎΡ‚Π°Π΅ΠΌ с ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ alert(myVariable["myVariable"]); // Π²ΠΎΡ‚ Ρ‚Π°ΠΊ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Π΅Π΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ function getName(arrayVariable){ // функция для извлСчСния ΠΈΠΌΠ΅Π½ΠΈ for (key in arrayVariable) return key; } alert(getName(myVariable)); // ΠΈΠ·Π²Π»Π΅ΠΊΠ°Π΅ΠΌ имя </script> 
    • oh, what a bad example :-) why is there an array, if it is used as an object? :) - Grundy
    • @Grundy in this context, if you wrap a variable in an object, then nothing will change, as it seems to me. - perfect
    • that is what it is about, and since it is used only as an object, it is necessary to create an object, not an array - Grundy
    • @Grundy Well, if you consider this a matter of principle, then I will correct the code, otherwise someone has already succumbed to your trend and put a minus) - perfect
    • Yeah :-) I put it :) well, the {} literal {} better to use in this case - Grundy

    The variable does not store such information about itself, which means you will not be able to get its string representation of the name.

    The maximum that you can do is to know the context of a variable and try to get its name through the parent. And that, if only this variable is reference object, instead of a value variable.

     function context(){ this.a = { "test" : "test" }; for(key in this){ if(this[key] == a) console.log(key); } } context(); 

      This can be done, but the variable must be an object, and it must be given a unique property or method by which it can be distinguished in the list of other variables. You can get and sort through the list of variables among which the required one is found, inside the document you can

       for (var variable in window) { //Анализ variable - она или нС она } 

      Inside you need to use eval and typeof to access the object by its name and determine the presence of a unique property / method. It loads the client’s browser a bit, but it works. Tested in Fox.

      Here is an example.

       var Class = function() { } Class.prototype.isOurVariable = function() {return true}; var object123 = new Class(); //Π˜Ρ‰Π΅ΠΌ имя этой ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ for (var variable in window) { if (!/[az]+/.test(variable)) { //отсСкаСм свойства Ρ‚ΠΈΠΏΠ° "0", Π΅ΡΡ‚ΡŒ Ρ‚Π°ΠΊΠΈΠ΅ continue; } eval("var ok = (" + variable + " != null && typeof (" + variable + ".isOurVariable) != 'undefined');"); if (ok) {console.log(variable); break} } 
      • and if the variable is not in the window? - Grundy Nov.
      • and the string eval, is quietly replaced by: var ok = (window[variable] != null && typeof (window[variable].isOurVariable) != 'undefined'); - Grundy Nov.
      • I have everything in the window. - nikolay yudin
      • but i haven't. Any local variable in the function is not added to the window. Therefore, it is worth either adding these restrictions in response, or deleting it - Grundy
      • Even simply replacing var object123 with let object123 - the code breaks and does not work - Grundy