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
  • 123,782
  • 12
  • 99
  • 138
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
  • 16,564
  • 7
  • 51
  • 69
enno.void
  • 5,615
  • 4
  • 24
  • 39
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
  • 686
  • 4
  • 18
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
  • 900
  • 3
  • 10