Named Function Expression
Last Updated :
30 Jul, 2024
In JavaScript or in any programming language, functions, loops, mathematical operators, and variables are the most widely used tools. This article is about how we can use and what are the real conditions when the Named function Expressions. We will discuss all the required concepts in this article to know the named function Expression in and out.
Named function: Using the function keyword followed by a name that can be used as a callback to that function is known as using a named function in JavaScript. Named functions are regular functions that have a name or identifier. Both their use in expressions and their declaration in statements are options. The name of the function is stored in the body, which is useful. Additionally, we might use the name to get a function to call itself or to acquire its characteristics, just like we would for any object.
Syntax:
var functionName(){
// Code here
}
Function Expression: The primary distinction between Function Expression and Function Declaration is the ability to define anonymous functions without function names using Function Expression. An IIFE (Immediately Invoked Function Expression) is a function expression that executes as soon as it is defined. The variableName keyword can be used to access a function expression, which must be kept in a variable. It is now simpler to declare function expressions thanks to the ES6 capabilities that introduce Arrow Function.
Syntax:
var variableName = function () {
// Code here
}
Named Function Expression: So, it is now obvious that the Function Expression which has a name or identifier is called Named Function Expression. It can be helpful that the function’s name is bound inside of its body. Additionally, we can access a function’s properties just like any other object by using its name to have it call itself.
Syntax:Â
var variableName = function f(){
// Code here
};
The f() function in the syntax example is available only inside the inner scope and to invoke the function outside the inner scope we need to call it with variableName.
What is the utility of Named Function Expression?
For recursive calls or distancing event listeners, access the designated function within its scope. When employing named functions, call stacks and error messages will display more specific information. As a result, the experience of debugging is enhanced by fewer anonymous stack names. Naming a function makes the code more readable, and clear, and helps you communicate your intentions.
Example 1: The code below demonstrates how we can create a recursive Named Function Expression and return a Fibonacci number.
JavaScript
<script>
var fibo = function fibonacci(number) {
if (number <= 1) return 1;
return fibonacci(number - 1) + fibonacci(number - 2);
};
console.log(fibo(5));
</script>
Output:
8
Example 2: The code below demonstrates the function that calculates the factorial of 6 and it gets called when a button is clicked.
HTML
<!DOCTYPE html>
<html lang="en">
<body>
<h1 style="color:green">GeeksforGeeks</h1>
<h2><b>Named function expression in JavaScript.</b></h2>
<div class="result"
style="font-size: 40px; color: green;">
</div>
<br />
<button class="btn">CLICK HERE</button>
<h3>Click on the button to call the factorial function
expression of obj object <br>And also get the
result of the factorial of 6.
</h3>
<script>
let ele = document.querySelector(".result");
let BtEle = document.querySelector(".btn");
let obj = {
factorial: function fact(n) {
if (n <= 1) {
return 1;
}
return n * fact(n - 1);
},
};
BtEle.addEventListener("click", () => {
ele.innerHTML = obj.factorial(6);
});
</script>
</body>
</html>
Output:

Similar Reads
JavaScript Function Expression
A function expression is a way to define a function as part of an expression making it versatile for assigning to variables, passing as arguments, or invoking immediately. Function expressions can be named or anonymous.They are not hoisted, meaning they are accessible only after their definition.Fre
3 min read
JavaScript function* expression
The function* is an inbuilt keyword in JavaScript which is used to define a generator function inside an expression. Syntax: function* [name]([param1[, param2[, ..., paramN]]]) { statements}Parameters: This function accepts the following parameter as mentioned above and described below: name: This p
2 min read
Rational Expression
A rational expression is essentially a fraction where both the numerator and the denominator are polynomials. This means it is an algebraic expression that can be written in the form [Tex]\frac{P(x)}{Q(x)}= \frac{Polynomial}{Polynomial}[/Tex]where P(x) and Q(x) are polynomials, and Q(x) â 0 (since d
8 min read
TypeScript Function Type Expressions
In this article, we are going to learn about TypeScript Function Type Expressions in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, a function type expression represents the type of a function, including its parameter types
3 min read
Expressions in Math
Expressions in math are combinations of numbers, variables, operators, and sometimes parentheses that represent a particular value. An expression is a statement involving at least two different numbers or variables and at least one operation, such as addition, subtraction, multiplication, division,
7 min read
Expressions in Python
An expression is a combination of operators and operands that is interpreted to produce some other value. In any programming language, an expression is evaluated as per the precedence of its operators. So that if there is more than one operator in an expression, their precedence decides which operat
5 min read
Lambda Expressions in JavaScript
A lambda expression is a concise way to define a short function in programming. It's commonly found in modern languages like Python, Ruby, JavaScript, and Java. Essentially, it's just a small piece of code that creates a function. Thanks to functions being treated as objects in JavaScript, they can
1 min read
JavaScript yield* Expression
The yield* expression in JavaScript is used when one wants to delegate some other iterable object. This function iterates over the particular operand and yields each value that is returned by it. Syntax: yield* expression; Return Value: It returns the iterable object. Example 1: In this example, we
2 min read
XPath Expression
The XPath Expression is an expression that is used to select nodes in the XML element. These expressions are similar to the path expression in our system files path. XSLT performs transformation based on this expression. Following is the table of some useful expressions to select any node in the XML
3 min read
PHP match Expression
The PHP match expression is used for the identity check of a value. It is similar to the switch statement i.e. it matches the expression with its alternative values. The match expressions are available in PHP 8.0.0. The match expression compares the value using a strict comparison operator (===) whe
1 min read