How to turn a String into a JavaScript function call?


I got a string like:

settings.functionName + '(' + + ')';

that I want to translate into a function call like so:


This of course will have to be done in JavaScript. When I do an alert on settings.functionName + '(' + + ')'; it seems to get everything correct. I just need to call the function that it would translate into.


settings.functionName = clickedOnItem = IdofParent
7/23/2017 3:13:52 PM

Accepted Answer

Seeing as I hate eval, and I am not alone:

var fn = window[settings.functionName];
if(typeof fn === 'function') {

Edit: In reply to @Mahan's comment: In this particular case, settings.functionName would be "clickedOnItem". This would, at runtime translate var fn = window[settings.functionName]; into var fn = window["clickedOnItem"], which would obtain a reference to function clickedOnItem (nodeId) {}. Once we have a reference to a function inside a variable, we can call this function by "calling the variable", i.e. fn(, which equals clickedOnItem(, which was what the OP wanted.

More full example:

/* Somewhere: */
window.settings = {
  /* [..] Other settings */
  functionName: 'clickedOnItem'
  /* , [..] More settings */

/* Later */
function clickedOnItem (nodeId) {
  /* Some cool event handling code here */

/* Even later */
var fn = window[settings.functionName]; 
/* note that settings.functionName could also be written
   as window.settings.functionName. In this case, we use the fact that window
   is the implied scope of global variables. */
if(typeof fn === 'function') {
2/21/2020 10:19:27 PM


No need for an eval()


Here is a more generic way to do the same, while supporting scopes :

// Get function from string, with or without scopes (by Nicolas Gauthier)
window.getFunctionFromString = function(string)
    var scope = window;
    var scopeSplit = string.split('.');
    for (i = 0; i < scopeSplit.length - 1; i++)
        scope = scope[scopeSplit[i]];

        if (scope == undefined) return;

    return scope[scopeSplit[scopeSplit.length - 1]];

Hope it can help some people out.


JavaScript has an eval function that evaluates a string and executes it as code:

eval(settings.functionName + '(' + + ')');

eval() is the function you need to do that, but I'd advise trying one of these things to minimize the use of eval. Hopefully one of them will make sense to you.

Store the function

Store the function as a function, not as a string, and use it as a function later. Where you actually store the function is up to you.

var funcForLater = clickedOnItem;

// later is now


someObject.funcForLater = clickedOnItem;    
// later is now    

Store function name

Even if you have to store the function name as a string, you can minimize complexity by doing


which minimizes the amount of Javascript you have to construct and eval.

Dictionary of handlers

Put all of the action functions you might need into an object, and call them dictionary-style using the string.

// global
itemActions = { click: clickedOnItem, rightClick: rightClickedOnItem /* etc */ };

// Later...
var actionName = "click"; // Or wherever you got the action name
var actionToDo = itemActions[actionName];

(Minor note: If instead here you used syntax itemActions[actionName](; then the function would be called as a method of itemActions.)


While I like the first answer and I hate eval, I'd like to add that there's another way (similar to eval) so if you can go around it and not use it, you better do. But in some cases you may want to call some javascript code before or after some ajax call and if you have this code in a custom attribute instead of ajax you could use this:

    var executeBefore = $(el).attr("data-execute-before-ajax");
    if (executeBefore != "") {
        var fn = new Function(executeBefore);

Or eventually store this in a function cache if you may need to call it multiple times.

Again - don't use eval or this method if you have another way to do that.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]