John Doe
John Doe

Reputation: 39

Javascript variable issue

why does this

http://jsfiddle.net/BkGxq/1/

return "$undefined" ?

if i dont do

 level_prices['f'.i] = i;

and do

 level_prices[i] = i;

it works, (i also change it in the html to value="3" rather then value="f3"

but i need to access it as f3 and not 3, why does it not work?

Upvotes: -1

Views: 39

Answers (2)

Bergi
Bergi

Reputation: 664207

The dot is not the string concatenation operator, but the property accessor. You want

level_prices['f'+i] = i;

'f'.i gets the literal "i" property of the string object, which is undefined.

Upvotes: 2

KaeruCT
KaeruCT

Reputation: 1645

In JavaScript, you concatenate strings with + instead of .

Try:

level_prices['f'+i] = i;

Edit: updated your JSFiddle: http://jsfiddle.net/BkGxq/2/

Upvotes: 0

Related Questions