1

so i was trying to make custom variables generator but i couldn't figure out the way to create custom variavle with name from string it keeps sending error: Uncaught SyntaxError: Unexpected token '['

can someone help my code look like this:

var tempi = "csv"
var window[tempi]={0:"r"};

and i expect it to do

var csv ={0:"r"}

and create with this multiple variables with diffrent names

luktvpl
  • 21
  • 1
  • 3
  • It's not clear to me what the intent here is. Are you expecting after this code executes that there would be a locally-scoped variable called `windowcsv`? Something else? – David Jan 18 '22 at 18:12
  • i expect it to do : var csv ={0:"r"} – luktvpl Jan 18 '22 at 18:19

2 Answers2

0

Your code looking good. It seems that you dont define window['csv'] previously. Take a look on this example:

tempi = 'csv'
data = [];
data['csv'] = {0: 'hello'};

console.log(data[tempi])
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
0

The error is that you are using the var initializer word for window, but window is already defined when program loads, so just avoid it:

var tempi = "csv"

window[tempi] = {
    0: "r"
};

console.log(csv)
AlexSp3
  • 2,201
  • 2
  • 7
  • 24