9
var abc = '123'

var def = '456'

need to write somefuntion() which convert variable name into string

like

alert ("value of" +somefuntion(abc) +"is"+ abc +"and value of" +somefuntion(def)+" is"+def );

should be like this

alert ("value of abc is 123 and value of def is 456")

i have searched and tried few solution and idk how this link can help cuz i know you will say its duplicate but i tried it and it didnt work

How to turn variable name into string in JS?

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
Ahtesham ul haq
  • 319
  • 1
  • 5
  • 14
  • String literals? – Ajay Gaur Nov 24 '17 at 07:36
  • Use objects. I dont think you can do that with literal variables. Try `var data = { abc: '123', 'def': '456'}; var s = ''; for (var k in data) { s += 'value of ' + k + ' is ' + data[k];}` – Rajesh Nov 24 '17 at 07:36
  • 3
    It's impossible with plain variables. You could use anything else depending on your use case. – Dane Nov 24 '17 at 07:37
  • I found the answers [here](https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript) to be very useful. – Waleed Iqbal Nov 24 '17 at 07:55

3 Answers3

38

Use the code below.

const abc = '123';
const def = '456';

const varToString = varObj => Object.keys(varObj)[0]

alert ("value of " +varToString({def}) +" is "+ def +" and value of " +varToString({abc})+" is "+abc );

What this basicly does is, you pass the variable to varToString as an object using {variable}. varToString then returns an array with the passed variable as value.

[
  "abc"
]

We grab this value using the [0] selector (Object.keys(varObj)[0]). Now it returns

abc
Red
  • 6,599
  • 9
  • 43
  • 85
  • Thanks @Red , yes it strange requirement :) – Ahtesham ul haq Nov 24 '17 at 10:25
  • It is a strange requirement, for me too. Thanks for the question and thanks for the answer – Sanjeev Siva Apr 16 '19 at 09:39
  • what if i want to obtain the name of an object property ? is there any possible way to do so ? – Ceylan Mumun Kocabaş Sep 08 '21 at 11:37
  • @CeylanMumunKocabaş Yes, just use `object.keyName`. BTW `varToString()` accepts an `object` as parameter, so `varToString(object)` will return all `keys` in that *object*. – Red Sep 08 '21 at 14:18
  • well, i was lookin how to get the name of a function, then i learned the javascript function actually has a property `.name` which returns the string representation of the methods name (in most of the cases) . i needed this to pass an instance and the name of the function which belongs to the passed instance of an object. – Ceylan Mumun Kocabaş Sep 08 '21 at 14:52
  • If you provide me with an example at https://jsfiddle.net I can look at what I can do. – Red Sep 08 '21 at 14:57
1

Yes, you can do it:

function getVariableName(v) {
    for (var key in window) {
        if (window[key] === v)
            return key;
    }
}
var abc = '123';
var def = '456'
alert ("value of " + getVariableName(abc) +" is "+ abc +" and value of " + getVariableName(def) + " is " + def);
Gal Shaboodi
  • 744
  • 1
  • 7
  • 25
1

A neat way of doing this would be:

make an object as :

const data = {
    abc: {
        key: 'abc',
        value: '123',
    },
    def: {
        key: 'def',
        value: '456',
    }
}

and then when you want to access them, you can do :

alert ("value of" +data.abc.key +"is"+ data.abc.value  +"and value of" +data.def.key+" is"+data.def.value );

or

const abc = {key: 'abc', value: '123'};
const def = {key: 'def', value: '456'};

alert ("value of" +abc.key +"is"+ abc.value  +"and value of" +def.key+" is"+def.value );
Ajay Gaur
  • 5,140
  • 6
  • 38
  • 60