What is function.name in JavaScript?

The name property of the Function object is used to get the function’s name.

name is a read-only property of the Function object.

Syntax

function.name 

Code

Let’s observe several functions and use function.name to return their name.

console.log("\Normal Function")
console.log("-------------------------")
function test(){ }
console.log(test.toString(), "-- name -- ", test.name);
let fn1 = test;
console.log(fn1.toString(), "-- name -- ", fn1.name);
let fn2 = function(){ }
console.log(fn2.toString(), "-- name -- ", fn2.name);
console.log("\nFunction Expression")
console.log("-------------------------")
let fn3 = function f() { }
console.log(fn3.toString(), "-- name -- ", fn3.name);
console.log("\nObject function")
console.log("-------------------------")
let obj = { fobj() { } }
console.log(obj.fobj.toString(), "-- name -- ", obj.fobj.name);
console.log("\n with new Function constructor")
console.log("-------------------------")
let fn4 = (new Function); // "anonymous"
console.log(fn4.toString(), "-- name -- ", fn4.name);
console.log("\n Arrow function")
console.log("-------------------------")
let arrow = ( () => {} ).name;
console.log("Arrow function name", arrow); // empty string
console.log("((){}).name => ", ( () => {} ).name); // empty string
console.log("\n Bind Function")
console.log("-------------------------")
function fn5() { };
let bindFn = fn5.bind({});
console.log(bindFn.toString(), "-- name -- ", bindFn.name);

Explanation

The following explanations can provide a better understanding of the code above.

Function 1

The test.name will be test.

function test() {
}

Explanation 1

We used the test.toString method to print the function as a string.

Function 2

function test(){

}

let fn1 = test;
fn1.name; // test 

Explanation 2

We assigned the test function to an fn1 variable and accessed the name property through the fn1 variable like fn1.name. This will return test as the function name.

Function 3

let fn2 = function(){

}
fn2.name;

Explanation 3

We created an anonymous function and assigned it to a variable. Internally, the function name is inferred as the variable name. So, the fn2.name will hold fn2 as its value.

Function 4

let fn3 = function f(){

}

fn3.name;

Explanation 4

We created a function expression with the function name f and assigned it to an fn3 variable. Now, the fn3.name holds f as the value.


Function 5

let obj = {
  fobj(){
   
  }  
}
obj.fobj.name; // fobj

Explanation 5

We created an object that has the fobj function. The obj.fobj.name will hold fobj as the function name.

Function 6

let fn4 = (new Function);
fn4.name;

Explanation 6

We created a function with new Function, which will internally generate a function called anonymous. The fn4.name will hold anonymous as a value.

Function 7

let arrow = (()=>{}).name;
arrow;  // ""

Explanation 7

We created an arrow function. The name property of the arrow functions is an empty string.

Additionally, the name of the anonymous function is also an empty string if it is not assigned to a variable.

(function(){}).name; // ""

Function 8

function fn5() {};
let bindFn = fn5.bind({});

Explanation 8

We created an fn5 function and bound it to an empty object. For the bind function, JavaScript will add the word bound before the function name.

In our case, the bindFn.name will have bound fn5 as a value.

Free Resources