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
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments