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

window['variable_name']

// For function
window['function_name'](parameters)
Here is some examples, see the explanations in code.

Using variable with name stored into a string

var vr_name = 'vr1'; // string with name of a variable

var vr1 = 'coursesweb.net/';

// 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
document.write(site); // https://coursesweb.net/

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 = 'coursesweb.net/';
var vr2 = 'gamv.eu/';

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

// Test
document.write(site); // https://gamv.eu/

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
document.write(sum); // 35

Calling 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
document.write(hi); // Hello dear ME

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
Using Variable and Function with Name from String in JavaScript

Last accessed pages

  1. Star shapes with CSS (5510)
  2. querySelector and querySelectorAll (12510)
  3. A simple script ActionScript 3 (3151)
  4. Node.js Move and Copy file (13895)
  5. Contact page - CoursesWeb (27508)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1588)
  2. Contact page - CoursesWeb (1551)
  3. Tabs effect with CSS (1545)
  4. PHP getElementById and getElementsByTagName (750)
  5. Get Attribute (ID, Class, Name, Title, Src) with jQuery (625)