JacksonML
JacksonML

Reputation: 29

Trying to take a variable's value and make it the name of another variable. JavaScript

I'm trying to take a function's input (objectName) and make that value create a variable. So if objectName = "abcd" I would want the variable to be created be abcd. Is there a way I can do this in JavaScript?

Upvotes: 0

Views: 64

Answers (1)

Shryme
Shryme

Reputation: 1570

You can use window[variableName] like this:

//Variable name
var name = "abc";

//window[name] is equal to the variable abc
window[name] = 123;

//show 123
alert(abc);

FIDDLE

Upvotes: 5

Related Questions