Skip to content
Related Articles

Related Articles

Improve Article

JavaScript Function.displayName Property

  • Last Updated : 23 Aug, 2020

Below is the basic example of Function.displayName property.




   
<script>
    function func1(){}
    func1.displayName="someName"
    console.log(func1.displayName)
</script>

Output:

someName 

The Function.displayName property in JavaScript is used to set the display name of the function. If the displayName property is used to log the name without setting the displayName property of the function than the output will be undefined.

Syntax:

function.displayName = name

Return Value: It returns nothing instead, it sets the display name of the function.



Note: By default, the display name of the function is undefined.

Few examples are given below for a better understanding of the function.displayName property.

Example 1:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
</head>
  
<body>
    <script>
  
        // Creating function name func1
        function func1() {
  
            // Logging to console
            console.log("This is from function 1")
        }
  
        // Changing the func1 name to function1
        // using the function.displayname
        func1.displayName = "function1"
        console.log("Display name of the function"
                + " func1 is :", func1.displayName)
    </script>
</body>
  
</html>

Output:

Example 2:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
</head>
  
<body>
    <script>
  
        // Creating function name func
        function func() { }
  
        // Changing the func name to function1
        // using the func.displayname
        func.displayName = "function1"
        console.log("function is :", func)
  
        // Logging name of the function
        // using function.name property
        console.log("Name of the function "
                + "func is :", func.name)
        console.log("DisplayName of the "
                + "function func is :", 
                func.displayName)
    </script>
</body>
  
</html>

Output:

Browsers Supported:.

  • Google Chrome
  • Mozilla Firefox



My Personal Notes arrow_drop_up
Recommended Articles
Page :