Concatenate a dynamic variable name in Javascript

javascript, variables

Solution

Not sure exactly what you are trying to accomplish, but with JavaScript, you could use the following:

> var a = 1;
> var b = {};
> b['fruit_' + a] = 'apple';
> b.fruit_1
"apple"
> b['fruit_1']
"apple"

Problem

In PHP you can concatenate a variable name like this ``` $a = 1 ${'fruit_' . $a} = 'apple'; ``` The result will lead to the creation of variable `$fruit_a` How would I do this in javascript?

Original source