How to use variable instead of function name in JavaScript

Use .call()

function callme(a,b){
  return, b);

var func = function(c) {
  return c;

alert(callme(func, "test"));

Any function you pass to callme will execute there. The first argument is the context, so you could pass this if you need to. Pass null otherwise.

In your case, alert/confirm can be passed. If you don’t need to return something, just do an empty return at the end.

Don’t use eval, for the love of all things good.

Browse More Popular Posts

Leave a Comment