JavaScript Function name Property

Below is a Basic example of Function name Property.

  • Example: 

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  // Creating function name func1
  function func1(){
  }
  document.write(func1.name)
</script>

chevron_right


  • 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

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

obj[object Object]is an instance of function func

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

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // Function func
    function func() {};
    // Logging bounded function to console.
    document.write("Name of the bounded func is: "
    ,func.bind({}).name)
  </script>

chevron_right


Output:

Name of the bounded func is: bound func

Supported Browsers:  

  • Google Chrome
  • Firefox
  • Safari
  • Internet Explorer
  • Microsoft Edge
  • Opera

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai