# Variables based off user input

**URL:** <https://forum.code.org/t/variables-based-off-user-input/37192>\
**Category:** Uncategorized\
**Tags:** applab\
**Created:** [November 6, 2022, 10:51pm UTC](https://forum.code.org/t/variables-based-off-user-input/37192 "2022-11-06T22:51:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![aiden1](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aiden1](https://forum.code.org/u/aiden1)\
**Post date:** [November 6, 2022, 10:51pm UTC](https://forum.code.org/t/variables-based-off-user-input/37192/1 "2022-11-06T22:51:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wutadam](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/wutadam/32/7711_2.png) [@wutadam](https://forum.code.org/u/wutadam)\
**Post date:** [November 7, 2022, 2:14am UTC](https://forum.code.org/t/variables-based-off-user-input/37192/2 "2022-11-07T02:14:45Z")

</div>

You could use prompts:

```javascript
var result = prompt("Prompt text");

```

---

<div class="post-metadata">

**Author:** ![aiden1](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aiden1](https://forum.code.org/u/aiden1)\
**Post date:** [November 7, 2022, 3:58am UTC](https://forum.code.org/t/variables-based-off-user-input/37192/3 "2022-11-07T03:58:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ismailmf777](https://avatars.discourse-cdn.com/v4/letter/i/d9b06d/32.png) [@ismailmf777](https://forum.code.org/u/ismailmf777)\
**Post date:** [November 7, 2022, 9:37am UTC](https://forum.code.org/t/variables-based-off-user-input/37192/4 "2022-11-07T09:37:41Z")

</div>

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

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

```

There is another solution using the eval() function.

```auto
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.

---

<div class="post-metadata">

**Author:** ![aiden1](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aiden1](https://forum.code.org/u/aiden1)\
**Post date:** [November 7, 2022, 4:17pm UTC](https://forum.code.org/t/variables-based-off-user-input/37192/5 "2022-11-07T16:17:10Z")

</div>

Ok thanks I’ll try that

---

<div class="post-metadata">

**Author:** ![islanderjourney](https://avatars.discourse-cdn.com/v4/letter/i/f04885/32.png) [@islanderjourney](https://forum.code.org/u/islanderjourney)\
**Post date:** [November 7, 2022, 9:37pm UTC](https://forum.code.org/t/variables-based-off-user-input/37192/6 "2022-11-07T21:37:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aiden1](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@aiden1](https://forum.code.org/u/aiden1)\
**Post date:** [November 8, 2022, 4:11am UTC](https://forum.code.org/t/variables-based-off-user-input/37192/7 "2022-11-08T04:11:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![walker200706](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/walker200706/32/8447_2.png) [@walker200706](https://forum.code.org/u/walker200706)\
**Post date:** [September 28, 2023, 4:31pm UTC](https://forum.code.org/t/variables-based-off-user-input/37192/8 "2023-09-28T16:31:09Z")

</div>

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:

> **[Check out what I made](https://studio.code.org/projects/gamelab/bHOdK99tjr2JIngDHM7FMJElIES96_XIU-NL6irNOtI)**
>
> I wrote the code myself with Code.org

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”.

---

<div class="post-metadata">

**Author:** ![walker200706](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/walker200706/32/8447_2.png) [@walker200706](https://forum.code.org/u/walker200706)\
**Post date:** [September 28, 2023, 4:32pm UTC](https://forum.code.org/t/variables-based-off-user-input/37192/9 "2023-09-28T16:32:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![letti42](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@letti42](https://forum.code.org/u/letti42)\
**Post date:** [October 3, 2023, 2:29am UTC](https://forum.code.org/t/variables-based-off-user-input/37192/10 "2023-10-03T02:29:54Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![varrience](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/varrience/32/8042_2.png) [@varrience](https://forum.code.org/u/varrience)\
**Post date:** [October 4, 2023, 2:51am UTC](https://forum.code.org/t/variables-based-off-user-input/37192/11 "2023-10-04T02:51:44Z")

</div>

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

```auto
// 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
