DEV Community

jkaplan15
jkaplan15

Posted on

Understanding the difference between Named Functions and Anonymous Functions and when to use each of them

Functions are an essential part of programming, and they play a crucial role in Javascript. There are two types of functions: Named Functions and Anonymous Functions. At first glance you might think the difference is quite obvious and in theory it is. Named Functions contain a name and Anonymous Functions do not. But what is not obvious, is when to use each of the aforementioned functions. In this blog post we will explore the differences between Named and Anonymous functions and when to use each type.

What are Named Functions

A Named Function is a function that has a name and can be referenced by that name. This function is created by using the 'function' keyword, followed by the functions name, the parameter list and function body. Here's an example of a named function:

function greet(name) {
  console.log("Hello, " + name + "!");
}
Enter fullscreen mode Exit fullscreen mode

In this example, 'greet' is the function name and it takes one parameter, 'name'. When called, the function will log the message "Hello,[name]!" to the console, where '[name]' is replaced by the actual value passed in as an argument.

For example, if we call greet("Bill"), the function will output: "Hello, Bill!".

Named Functions are useful when you want to reference the function in your code multiple times or when the functions name provides additional information about its purpose.

What are Anonymous Functions

An Anonymous Function is a function that does not have a name. Anonymous functions are often used when you need to define a function inline or when you only need to use the function one time. Here is an example of an anonymous function:

let sum = function(a, b) {
   return a + b;
}
Enter fullscreen mode Exit fullscreen mode

This function (which does not have a name) is assigned to a variable called 'sum' and takes two parameters, 'a' and 'b'. When called the function returns the sum of 'a' and 'b'.

For example if we call sum(2, 3), the function will return 5.

When To Use Named Functions

Named Functions are useful when you need to reference the function in your code multiple times. They are also helpful when the function's name provides additional information about it's actual purpose. The following are some situations when you might want to use a named function:

  1. Reusability: If you need to use the function in multiple places throughout your code, a named function is more appropriate since it can be called by name.
  2. Debugging: Named functions are easier to debug than anonymous functions since their name appears in the stack trace when an error occurs.
  3. Clarity: Named functions can improve the readability and clarity of your code by providing meaningful names to the functions you define.

When to use Anonymous Functions

Anonymous functions are useful when you only need to use the function once or when the function's purpose is only relevant in a specific context. The following are some situations when you might want to use an anonymous function:

  1. Callbacks: Anonymous functions are commonly used as callbacks for asynchronous functions like 'setTimeout', 'setInterval', and 'addEventListener'. These functions often take a callback function as an argument, which is executed once the asynchronous operation is complete.
  2. Immediately Invoked Function Expressions (IIFE): Anonymous functions can be used to create an IIFE, which is a function that is executed as soon as it's defined. IIFEs can be used to create a private scope and avoid polluting the global namespace.
  3. Higher-order functions: Higher-order functions are functions that take other functions as arguments or return a function. Anonymous functions are often used as arguments for higher-order functions.

Conclusion

Named functions and anonymous functions both have their own use cases in JavaScript. Named functions are most useful when you need to reference the function in your code multiple times or when the function's name provides additional information about its purpose.

Sources:

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Both of the below examples are equivalent, they will both create a named function with the name sum:

// Example 1
function sum(a, b) {
  return a+b
}
console.log(sum(1, 2))  // 3
console.log(sum.name)  // 'sum' - the function has a name

// Example 2 - equivalent to example 1
let sum = function (a,b) {
  return a+b
}
console.log(sum(1, 2))  // 3
console.log(sum.name)  // 'sum' - the function STILL has a name
Enter fullscreen mode Exit fullscreen mode

The act of storing an anonymous function (defined with a function expression) in a variable makes the function (in most cases) cease to be anonymous.

An actual anonymous function (as the word implies) - has no name:

console.log((function (a,b) {
  return a+b
}).name)  // <empty string>
Enter fullscreen mode Exit fullscreen mode

We can also create a named function using a function expression, and store that in variable that has a different name to the function:

let myFunction = function sum(a, b) {
  return a+b
}
console.log(myFunction.name)  // 'sum'
Enter fullscreen mode Exit fullscreen mode

Take a look at this post for more discussion about anonymous functions in JS: