Get function name in JavaScript

function test() {  alert(arguments.callee.name); } 
b = test; 
b();

outputs “test” (in Chrome, Firefox and probably Safari). However, arguments.callee.name is only available from inside the function.

If you want to get name from outside you may parse it out of:

b.toString();

but I think name property of function object might be what you need:

alert(b.name);

this however does not seem work for IE and Opera so you are left with parsing it out manually in those browsers.

Leave a Comment