JavaScript Function name Property
Last Updated :
22 May, 2023
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
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
function func1() {
}
function func2(a, b) {
}
console.log( "Name of the function func2 is: "
, func1.name)
console.log( "Name of the function func2 is: "
, func2.name)
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
let obj = {
function1: function functionName1() { },
function2: () => {
console.log( "function2 is running" )
},
function3: () => {
obj.function2();
},
}
obj.function3()
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() { };
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() { };
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
Share your thoughts in the comments
Please Login to comment...