1

This is what I have attempted, and may give a better gist of the question I'm trying to ask:

var x = "run";
var y = "Function";
var xy = x + y;

function runFunction() {
    console.log("Function has been called.");
}

xy();

What am I doing wrong here?

4 Answers4

1

You could use eval(), but don't. Instead, store your functions in an object:

const functions = {
  greetingOne: () => console.log("Hello!"),
  anotherGreeting: () => console.log("Hi there!");
};

const f = "greetingOne";
functions[f]();
AKX
  • 152,115
  • 15
  • 115
  • 172
0

It is possible if the function lives on an object.

const obj = {
  runFunction: function() {console.log('hello')}
}

var x = "run";
var y = "Function";
var xy = x + y;

obj[xy]();
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
enno.void
  • 6,242
  • 4
  • 25
  • 42
0

You can call eval that run string as Javascript code

function runFunction() {
    console.log("Function has been called.");
}
functionName = 'runFunction'
eval(functionName + '()');
jacob galam
  • 781
  • 9
  • 21
0

All global functions stores in window object.

let first = "first";
let second = "Second";

let xy = first+second;

function firstSecond() {
    return "Hello World";
}


console.log(window[xy]());
sourav satyam
  • 980
  • 5
  • 11