How to get function name using object inside function in javascript?

In this post, You learn how to get function name from inside a function and outside a function.

Function can be created with name called named functions as seen below

function namedfunction() {
    // code inside function

Also, Function created without name called anonymous functions (without name)

 var anonymousFunction = function() {
    // code inside function

ES6 way to get function name

We can get function name using


function name property

ECMAScript 6 introduction Function object which has name attribute to give name of the function.

It is readonly property

Let’s see how to get named functions

function myfunction() {
  console.log("my function"); 
console.log(; //myfunction
console.log(; //myfunction property

We can also get function name using object

Let’s create a object of function.

Object has a constructor object which has a name property . This can also invoked with class prototype as seen below.

Here is an syntax


Here is an code to example get name of named function

function myfunction1() {
  console.log("myfunction1"); //Person
var mf = new myfunction1();
console.log(; //myfunction1
console.log(;  //myfunction1

Here is an code for example to get name of anonymous function

const myfunction=function() {
  console.log("my function"); 
console.log(; //myfunction
console.log(; //myfunction

How to get running function name

We have a function in javascript running, and want to get name of the function inside a body.

we can get name of the function in many ways

  • using arguments callee

arguments.callee returns function defination, from that defination, get function name using string functions.

Here is an complete code example

function myfunction() {
        var functName = arguments.callee.toString();
        console.log(functName); // prints function defination
        functName = functName.substr('function '.length);        
        functName = functName.substr(0, functName.indexOf('('));        

Another way is to get

In this example, Function is defined and called from outside global scope

function myfunction1() {
    console.log( );


How to get dynamic function names in javascript

As dynamic functions are created with different names dynamically

Let’s create a dynamic function with variable contains name of the function

Inside a function, you can access the name directly which returns the name of the function

var dynamicfunction = 'myfunction';

window[dynamicfunction] = function() {


You learned multiple ways to find the name of the function inside a running function and from global scope outside

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.