Dynamic variables names in javascript

In JavaScript, an object’s properties can be access either by obj.prop or obj['prop'].

In the global scope, all variables are children of the window property. So you’ll be able to do:

var variableDynamic="Test";
window['variableName' + variableDynamic] = 'your value';

Check out this fiddle.

However, if you’d like to limit the scope of this variable to a function, you would have to define a parent object and use this in place of window.

Leave a Comment