Skip to content
Related Articles

Related Articles

Improve Article
JavaScript Function name Property
  • Last Updated : 24 Aug, 2020

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
  • Firefox
  • Safari
  • Internet Explorer
  • Microsoft Edge
  • Opera



My Personal Notes arrow_drop_up
Recommended Articles
Page :