-3
var a = "aa", b= "bb",c ="cc", d="dd", e="ee";
array = [a,b,c,d,e] // outputs ["aa", "bb", "cc", "dd", "ee"];

However is there a possibility in javascript to convert the variables (a, b,c,d,e) into strings?

Like: "a", "b", "c", "d", "e"??

P.S: the array values could be dynamic as well or more than the length mentioned above.

Thanks for the help!!

3
  • 5
    They already are strings. What are you asking? Do you mean the variable names? Commented Mar 26, 2018 at 22:05
  • what is your question ? Commented Mar 26, 2018 at 22:08
  • sorry, my bad, yes I meant the variable names into string =, updated the Question Commented Mar 26, 2018 at 22:09

2 Answers 2

2

You could do this with ES6 shorthand property names and return array of strings.

let a = "aa", b= "bb",c ="cc", d="dd", e="ee";
let strings = Object.keys({a, b, c, d, e});
console.log(...strings)

Sign up to request clarification or add additional context in comments.

3 Comments

that would ideally work if there was a fixed length of values in that array. My array could have 10 -20 items in it. Object.keys(a,b,c,....) might make it cumberosme to add every variable value to convert it to string..?
Why not just use object instead of an array, then you have key and value?
I tried this: Object.assign({}, array);, //outputs to: {0: "aa", 1: "bb", 2: "cc", 3: "dd", 4: "ee"} it will still give me the values and not the variable names.
0

Something like this

var a = "aa", b= "bb",c ="cc", d="dd", e="ee";
var array = [a,b,c,d,e];
({a,b,c,d,e} = array)
var keys = Object.keys({a,b,c,d,e});
console.log(keys)
console.log(array)

2 Comments

sorry that may not work...same thing I mentioned above for @Nenad's answer. ({a,b,c,d,e} ) are just examples, the values could be more than 10 or 50, in this case ({a,b,c,d,e,......)} might not be faesible.
maybe If you explain the complete use case, we could help, like why do you need to do this, how its being used

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.