0

I have requirement to pass function name as parameter and execute it. Is there any way to achieve it in node js.

I tried below code. While making call to another function inside myFunc1 I want to call newFunct instead of fName. With below code I am getting error - TypeError: fName is not a function

var myFunction = 'newFunct';

    myFunc1(myFunction);
    
    function myFunc1(fName){
            fName('World'); // Here instead of calling fName it should call newFunct
    }
    
    function newFunct(val){
            console.log("Hello " + val);
    }
  • 1
    Is there a reason you cannot simply pass the function itself? In JavaScript, functions are just like any other value. So, if you do `myFunc1(newFunct)` it should work. – Bill Sourour Feb 22 '21 at 15:13
  • [Call function from string in nodejs](https://stackoverflow.com/questions/19433932/call-function-from-string-in-nodejs) – Andreas Feb 22 '21 at 15:15
  • @BillSourour The requirement is condition based. If certain condition is met I need to pass that corresponding function name which needs to be executed. – Amith Srinivasan Feb 22 '21 at 15:25

0 Answers0