DEV Community

Kuganesh S
Kuganesh S

Posted on

Difference Between named function and anonymous function in JS?

Named function:

  • A named function has an identifier (name) after the function keyword.
  • Easy to debug the errors because errors show the function name.
  • You can call the function before it is defined.
  • You can call it anywhere in the code for execution and reuse.
  • It is hoisted.
function add(a, b) {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Anonymous function:

  • An anonymous function has no name.
  • Usually it is assigned to a variable or used as a callback function.
  • hard to debug because stack traces show “anonymous”.
  • Commonly used with arrow functions (()=>{}).
  • It is not hoisted.
const add = function(a, b) {
  return a + b;
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)