1

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.

7
  • Your example code runs fine for me, are you sure that's exactly how it looks in your code? Is the value of serverID a 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 using eval 9 times out of 10 there's a better way to do what you're trying to do. Commented Apr 2, 2020 at 22:07
  • @JohnMontgomery When I log serverID in the console right before the eval it outputs just fine, so there isn't anything wrong with the variable. The eval function is exactly as in my code, except for changing variable names to make them more readable here. Commented Apr 2, 2020 at 22:13
  • But what is its value? Just because you can log it doesn't mean it's a valid variable name, you can log anything. Commented Apr 2, 2020 at 22:17
  • @JohnMontgomery the actual server ID is something like 694717308618187441, but I added 'settings' right before this line of eval code 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. Commented Apr 2, 2020 at 22:24
  • That's why it wasn't working then, JavaScript variable names must begin with $, _, or a letter. Commented Apr 2, 2020 at 22:28

3 Answers 3

2

I'm going to guess that the eval fails because your serverID is a number (or a string that represents a number), so the statement would look like var 123 = {'server': 'test'}, which would give a syntax error.

In any case, a simple alternative would be to create a property on an object instead of a variable. Something like:

var myVariables = {};
//...
myVariables[serverID] = {'server': 'test'};

You could even add it to the global object, if it makes sense for your situation and you really need a global variable.

Sign up to request clarification or add additional context in comments.

3 Comments

I think this should work. For some reason having the value of a property as the object I was trying to store never crossed my mind. Server IDs are numbers, but I did add letters to the end for naming purposes earlier in the code.
Got it. If you really wanted to keep your original approach, all you needed was to add letters (or _ or $) at the beginning, because a variable identifier can't start with a number.
I ended up making an object with names of the server IDs and values of the original object I wanted to store. For some reason I had forgotten you could store objects inside properties of other objects.
0

There is no good reason to to store a variable as the name of another variable. The simplest way to accomplish what you want, as you mentioned, is storing the serverID in the object.

1 Comment

Hmmm, not exactly sure what you mean by 'storing the serverID in the object." In that case, what would be the name of said object and then what would happen to the name:value pair I have inside at the moment.
0

Your syntax is correct, although you may want to ensure that your server ID is a legal JavaScript identifier.

var serverID = "asddkjkisdf";
eval("var " + serverID + " = {'server': 'test'}")

console.log(asddkjkisdf);

I would however recommend creating a data structure like an array of objects to store this information. In that case you wouldn't have to worry about the value of your server ID. E.g.

[{id: 'asdfasdf', server: 'test'}, ... ]

1 Comment

This is the issue with my code. Because serverID started with a number, it wasn't a valid JavaScript variable name. Didn't think to check that earlier...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.