To access and use a variable or function dinamically, with Name from a String in JavaScript, just use the window object, with this syntax:

// For function
Here is some examples, see the explanations in code.
- Click on the code to select it.

1. Using variable with name stored into a string.
var vr_name = 'vr1';    // string with name of a variable

var vr1 = '';

// uses variable with name stored as string in vr_name
// window[vr_name] /window["vr1"] is the value of vr1
var site = 'http://'+ window[vr_name];

// Test
alert(site);    //

2. Using variable dinamically, with name stored into a string in object.
// object with strings with variable names
var vr_name = {'v1':'vr1', 'v2':'vr2'};
var vr1 = '';
var vr2 = '';

// uses variable dinamically, with name stored as string in vr_name
var i = 2;
var site = 'http://'+ window[vr_name['v'+ i]];

// Test
alert(site);    //

3. Calling function with name stored into a string.
function f1(a, b) {
  return a + b;

var f_name = 'f1';    // string with function name

// uses function with name stored as string in f_name
// window[f_name](parameters) calls the f1(parameters)
var sum = window[f_name](12, 23);

// Test
alert(sum);    // 35

4. Calling an object method with name stored in string into an object (use: object["method_name"](parameters)).
// object with strings with method names
var methods = {'m1':'hi1', 'm2':'hi2'};

// object with two methods
var obj = {
  'hi1': function(name) {
    return 'Hello dear '+ name;
  'hi2': function(name) {
    return 'Hola '+ name;

// uses object method with name stored as string in methods
var hi = obj[methods['m1']]('ME');

// Test
alert(hi);    // Hello dear ME

Daily Test with Code Example

Which tag is used in <table> to create table header cell?
<thead> <th> <td>
  <th>Title 1</th>
  <th>Title 2</th>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){"");
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
Using Variable and Function with Name from String in JavaScript

Last accessed pages

  1. jQuery Drag and Drop Rows between two similar Tables (6738)
  2. Basic HTML elements (3681)
  3. Sport Games (12516)
  4. JQZoom Image Magnifier (9297)
  5. Magic Treasure (283)

Popular pages this month

  1. Flash Games - Free online Games (1536)
  2. Qwop (1269)
  3. Tunnel Rush (1133)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (1092)
  5. Drag Racer V3 (823)