I want to be able to basically do this
let x = 1;
let `arr${x}`;
if ((i = 0)) `arr${x}` = [];
`arr${x}`.push(words);
console.log(`arr${x}`);
I have tried using eval()
let x = 1;
eval(`let arr${x}`);
if ((i = 0)) eval(`arr${x} = [];`);
eval(`arr${x}.push(${words})`);
console.log(eval(`arr${x}`));
But it's giving an error:
Uncaught ReferenceError: arr1 is not defined
In this line: eval(arr${x}.push(${words}));
evalis the only way, JS doesn't have a concept of dynamically created variable names. I'd suggest you to use a suitable data structure instead (ex. object). Notice, thati = 0in the conditions doesn't do what you think it does.