Javascript Course

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.

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

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 = 'https://'+ window[vr_name['v'+ i]];

// Test
document.write(site); //

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

Which tag is a block element?
<div> <img> <span>
<div>Web Programming and Development</div>
Which CSS code displays the text underlined?
font-style: italic; text-decoration: underline; font-weight: 500;
h2 {
  text-decoration: underline;
Click on the JavaScript function that can access other function after a specified time.
insertBefore() setTimeout() querySelector()
function someFunction() { alert(""); }
setTimeout("someFunction()", 2000);
Click on the instruction that returns the number of items of a multidimensional array in PHP.
count($array) count($array, 1) strlen()
$food =["fruits" =>["banana", "apple"), "veggie" =>["collard", "pea"));
$nr_food = count($food, 1);
echo $nr_food;       // 6
Using Variable and Function with Name from String in JavaScript

Last accessed pages

  1. The Prayer of the Frog (553)
  2. innerHTML in PHP (12004)
  3. Send POST data with file_get_contents (1559)
  4. Recursive function to create Multi-Level Menu in PHP (10059)
  5. PHP Method Chaining (3154)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (738)
  2. SHA256 Encrypt hash in JavaScript (672)
  3. PHP Unzipper - Extract Zip, Rar Archives (643)
  4. Read Excel file data in PHP - PhpExcelReader (640)
  5. Create simple Website with PHP (631)