Computer >> Computer tutorials >  >> Programming >> Javascript

How to execute a JavaScript function when I have its name as a string?


To execute a JavaScript function when its name is as a string, you can access class methods by name

Example

Live Demo

<html>
   <body>
      <script>
         class Demo {
            methodOne(){
               document.write("50");
            }
            methodTwo(){
                this['methodOne']();
                document.write("<br> 100");
            }
         }
         let num = new Demo();
         num['methodTwo']();
      </script>
   </body>
</html>