JavaScript Function name Property

02 Dec, 2021
Below is a Basic example of Function name Property.
Example: 
Javascript
<script>  // Creating function name func1  function func1(){  }  document.write(func1.name)</script>
Output:
func1

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
Returns: This property returns the string i.e the name of the function.
Code for the above property is provided below:
Program 1: When the simple function is given
HTML
<script>    // Creating function name func1    function func1(){    }    // Creating function name func2    function func2(a,b){    }    document.write("Name of the function func2 is: "    ,func1.name +"<br>")    document.write("Name of the function func2 is: "    ,func2.name +"<br>")        // Logging return type to console    document.write("Type of func.name is: "    ,typeof(func2.name))</script>
Output:
Name of the function func2 is: func1
Name of the function func2 is: func2
Type of func.name is: string
Program 2: When an object of function is given.
HTML
<script>    // 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    document.write("Name of the function function1 is: "                ,obj.function1.name+"<br>")    document.write("Name of the function function3 is: "                ,obj.function3.name+"<br>")</script>
Output:
Name of the function function1 is: functionName1
Name of the function function3 is: function3
Program 3: Using the name property on an instance of the function.
HTML
<script>    // Function func    function func() {};    // Obj is the instance of the     // function object func    let obj = new func();    if (obj.constructor.name === "func")      document.write("obj",obj,      "is an instance of function func")    else      document.write('Oops!')</script>
Output:
obj[object Object]is an instance of function func
Program 4: Using the name property on the bounded function.
HTML
<script>    // Function func    function func() {};    // Logging bounded function to console.    document.write("Name of the bounded func is: "    ,func.bind({}).name)  </script>
Output:
Name of the bounded func is: bound func
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
Akanksha_Rai
ysachin2314
JavaScript-Properties
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
Roadmap to Learn JavaScript For Beginners
JavaScript | Promises
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?