0

I was wondering if it is possible to create variable names from parameters passed to a function in javascript. Something like this:

function createVar(number) {
var "number" + number;
}
createVar(1)

I'm new to Stack Overflow and programming, so any help would be appreciated.

6
  • No, that is not possible. Commented Jan 25, 2015 at 19:57
  • 1
    @FelixKling Yes it is. Commented Jan 25, 2015 at 19:57
  • 4
    This seems to be an XY problem. Please tell us what you're actually trying to accomplish. Commented Jan 25, 2015 at 19:58
  • 1
    @minseong ohaidere. I think it's possible, but it's probably better to use an array Commented Jan 25, 2015 at 19:59
  • Why would this be useful anyway? Commented Jan 25, 2015 at 20:01

3 Answers 3

0

You could attach this to the window object, but note it will be global. For example:

function createVar(varName, value) {
    window[varName] = value;
}

createVar("test", "Hello World");
alert(test); // Outputs "Hello World".
Sign up to request clarification or add additional context in comments.

2 Comments

Why not assign it to an Object?
@cyan Simplicity, because the OP probably want's the access the variable by just it's name. For example test other than myObj.test or myObj["test"].
0

It is possible to interpret Object as associative array where you specify index and get value by name of index ( hash ):

var x = Array();
x[number] = value;

Single variable name is for programmer, and the code would be hard to maintain and understand when you set variable dynamically in code.

1 Comment

Note that there are no "associative arrays" in JavaScript.
0

Honestly, I don't see why this would ever be useful, because every time you want to use the variable you'd have to search for it with your number argument.

However, you can do it, albeit not the exact way you had described:

function createVar(number){
  eval("var number" + number.toString() + ";");
}

however, this variable will only be accessible within the function, to make it global assign to the window object:

function createVar(number){
  window["number" + number] = 15; // creates "global" variable
}

As I've stated before, however, I don't see this being useful, [i]ever[/i], if you want to stratify values by numbers you'd be much better off with an array.

4 Comments

The "global variable" case can be solved better with window['number' + number] = 15;.
@FelixKling Oh, I see - does that avoid the eval?
In that case yes. Doesn't work for local variables of course. eval would be the only way for local variables, which is definitely the wrong thing to do anyway, that's why I said it's not possible ;) Knowing the actual problem the OP is trying to solve would allow us to provide an actual solution.
@FelixKling Yeah, I agree. I'll quickly hop in and edit my answer.

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.