Variables based off user input

Is it possible to create new variables based off of user input? Ex: when a user enters something in a text box a new variable gets created that is named whatever they put in the text box.

You could use prompts:

var result = prompt("Prompt text");

No what I am trying to do is to make a new variable where the variable name is based off of user input

Yes, it is possible, but slightly complicated. Here is one way you can do it, using ‘this’.

this[prompt("Enter variable name")]=prompt("Enter variable value");

There is another solution using the eval() function.

eval("var "+prompt("Enter variable name")+"="+prompt("Enter variable value"));

If you aren’t in strict mode, you can omit the “var” part in the eval function because variables that are assigned values without been declared, automatically become global variables.

Both of these solutions do the same thing, by the way.

Ok thanks I’ll try that

A very simple way is to declare all the variables you need, but just declare them.
For example:
var Variable1;
var Variable2;
That way, you won’t worry about defining anything before user input.
Then when the user inputs something, you update the variable based on how you like.

yea but the thing is I need to create completely new variables basicly an infinite amount of times the user creates a new one. But the other answer should do ok. Thanks for responding though.

It seems like what you need is an “infinite variables solution”. I have been experimenting with groups and groups.add functions. Here is an example that I have made:

in this, you can create “infinite sprites”, but it isn’t exactly “infinite variables”. But you can indivdually delete the sprites, which kind of acts like “infinite variables”.

You can even go as far as to add values to each sprite

not sure why this conversation is being brought up but using global variables to handle this is really bad because its susceptible to attacks. especially using eval, a user can attack the program. Best way to do this is just using an array

var userVars = Array();
var user_input = prompt("new variable name here");
userVars[user_input] = "This is my new variable!";

Then you can call userVars[<variable name>] to get or set the value. Quite frankly if this solution doesn’t work for you then you should rethink how your program is being made. Generally variables should not be made by the user and then saved globally.

Actually, @letti42 we can sandbox such varriables within an object without using such eval methodology while maintaining private declaration

// use this for "unsafe" declaration
window[prompt("enter name")] = prompt("enter value");
// note: this is bad and can overwrite any global var within a program as letti has stated
// to eliminate that lets use a sandboxed enviroment
var inputs = {};
inputs[prompt("enter name")] = prompt("enter value")
// using this would be a much more acceptable approach
// however will need parsing if you plan on the variable holding a string value

anywho hope this helps