Open In App

JavaScript Function name Property

Last Updated : 22 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The function name property of the javascript object is used to return the name of the function. This name property of the function is only readable and cannot be altered. The name of the function which was given when the function was created is returned by Function.name.

Syntax:

Function.name

Property Values: This property has three attributes as mentioned below:

  • Writable: No
  • readable: No
  • configurable: Yes

Return Value: This property returns the string i.e the name of the function.

Example: Below is a Basic example of the Function name Property.

Javascript




// Creating function name func1
function func1() {
}
console.log(func1.name)


Output:

func1

More examples for the above property are provided below:

Example 1: When the simple function is given

Javascript




// Creating function name func1
function func1() {
}
// Creating function name func2
function func2(a, b) {
}
console.log("Name of the function func2 is: "
    , func1.name)
console.log("Name of the function func2 is: "
    , func2.name)
 
// Logging return type to console
console.log("Type of func.name is: "
    , typeof (func2.name))


Output:

Name of the function func2 is: func1
Name of the function func2 is: func2
Type of func.name is: string

Example 2: When an object of function is given.

Javascript




// Creating object of functions
let obj = {
    function1: function functionName1() { },
 
    function2: () => {
        console.log("function2 is running")
    },
    function3: () => {
        obj.function2();
    },
}
obj.function3()
// Calling object function1 but function
// name is functionName1
console.log("Name of the function function1 is: "
    , obj.function1.name)
console.log("Name of the function function3 is: "
    , obj.function3.name)


Output:

Name of the function function1 is: functionName1
Name of the function function3 is: function3

Example 3: Using the name property on an instance of the function.

Javascript




// Function func
function func() { };
// Obj is the instance of the
// function object func
let obj = new func();
if (obj.constructor.name === "func")
    console.log("obj", obj,
        "is an instance of function func")
else
    console.log('Oops!')


Output:

obj[object Object]is an instance of function func

Example 4: Using the name property on the bounded function.

Javascript




// Function func
function func() { };
// Logging bounded function to console.
console.log("Name of the bounded func is: "
    , func.bind({}).name)


Output:

Name of the bounded func is: bound func

We have a complete list of Javascript Function methods, to check those please go through this Javascript Function Complete reference article.

Supported Browsers: 

  • Google Chrome 15 and above
  • Firefox 1 and above
  • Safari 6 and above
  • Microsoft Edge 14 and above
  • Opera 10.5 and above


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads