How to turn a String into a JavaScript function call?

To turn a string into a JavaScript function call, try to run the following code

Example

<html>
   <body>
      <script>
         function myFunction(argument) {
            alert('My function ' + argument);
         }
         functionName = 'myFunction';
         window[functionName]('is working.');
      </script>
   </body>
</html>
Updated on: 2026-03-11T23:14:17+05:30

569 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements