0

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?

1 Answer 1

5

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

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

2 Comments

If you are using JQuery, the command window.name will create a global property of the window. This can be manipulated the same way a variable can. You would reference it with window.abc but, I like Shyrmes way a little bit better :D
Wow that was pretty simple haha. Thanks for the help. Worked like a charm in my function.

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.