How to Get Function Name in JavaScript

Anika Tabassum Era Feb 02, 2024
How to Get Function Name in JavaScript

This tutorial demonstrates three ways to get a function’s name in JavaScript.

Get Function’s Name in JavaScript

In JavaScript, we have several ways to get the name of a function. However, often it is required to be well-defined regarding what is being performed.

Though code lines are a few, it has a significant application value. The debugging and revises are often implemented based on the functions.

Here, in our shown instances, we will demonstrate three ways to define the name of the specified function. First, we will get the name directly after the function declaration.

Later we will assign the function under an object, thus calling the object and function. Then, we will retrieve the function name.

And the most excellent way to get the function name is to use an instance of the function. And availing the constructor name for that instance will return the function name. So let’s check on them.

Get Function Name Right After Declaration

Here, we will initiate a function (with or without content). The most important part is the function name.

So, we will apply, and the name property will return functionName. Let’s visualize the task in the following code.

Code Snippet:

function foo() {
  var x = 1;



Get Function Name with Object

The following link will have a detailed discussion on function and the property.

In the case of retrieving the name of a function via an object, we initiate an object and then assign the function declaration as its content. Later, we call the object by the

Let’s run the following code for a better understanding.

Code Snippet:

var obj = {
  foo2() {
    var y = 5;



Get Function Name as a Constructor

We will define a function and later create an instance of that new function. By Doing this, we have created an object of that function that will to the function.

Now, if we perform, we will get the function name for which we created this instance. The codes will speak more logically, so let’s hop in!

Code Snippet:

function foo3() {
  var z = 10;

var instance = new foo3();


Anika Tabassum Era avatar Anika Tabassum Era avatar

Era is an observer who loves cracking the ambiguos barriers. An AI enthusiast to help others with the drive and develop a stronger community.

LinkedIn Facebook

Related Article - JavaScript Function