In the process of making a discord bot using node.js and discord.js, I've come to a point where I want to create a new variable, in this case an object with one property (the string "server" and some string for a value.) To name said new variable, I want to use the server ID of the server I'm referring to here, which is stored in another variable. What is a way I can do this?
So far I've tried
eval("var " + serverID + " = {'server': 'test'}"), which gave me a syntax error: invalid/unexpected token on the second plus sign (replacing the object with a string still gave me the same error). Everywhere I've looked hasn't been helpful in explaining what is wrong with the eval function, and I'm confused as to how I would do this another way.
In case the first thing that came to your mind was restructuring how I'm working with variables and the types I'm using, whatever this outputs must let me add more information to this variable, which at least in my mind restricts me to using Objects and adding properties. I also store this variable to a JSON file later in the code which also restricts me to using either Arrays or Objects.
serverIDa valid variable name? But honestly, I can't think of a practical use case for this that wouldn't be better served by storing it in an array or as an object property instead. As a general rule, if you're considering usingeval9 times out of 10 there's a better way to do what you're trying to do.serverIDin the console right before theevalit outputs just fine, so there isn't anything wrong with the variable. Theevalfunction is exactly as in my code, except for changing variable names to make them more readable here.evalcode for naming purposes.console.log(serverID)outputs 694717308618187441settings correctly, so I thought there's no reason why 694717308618187441settings wouldn't work as a variable name.$,_, or a letter.