Skip to content

Latest commit

 

History

History
108 lines (80 loc) · 3.41 KB

anonymous-named-arrow-functions.md

File metadata and controls

108 lines (80 loc) · 3.41 KB

In JavaScript, you can define functions in several ways, two of which are anonymous functions and named functions. Here's a comparison:

Anonymous Functions

An anonymous function is a function without a name.

// The anonymous function is passed as the first argument
  setTimeout(function() {
    console.log("This message is displayed after 3 seconds");
  }, 3000);

Pros:

  • They can be used for one-off tasks where a function isn't going to be reused.
  • They can be used in functional programming constructs like map, filter, reduce, etc.

Cons:

  • Debugging can be more difficult because the stack trace will not have a function name.
  • They cannot be called recursively because they have no name.

Named Functions

A named function is a function with a specific name.

function greet() {
  console.log("Hello, world!");
}

Pros:

  • They are hoisted, which means they can be called before they are defined in the code.
  • Debugging is easier because the function name appears in the stack trace.
  • They can be called recursively.

Cons:

  • They take up more memory because the function name is stored in memory.

Examples

An anonymous function being used in a map operation:

let numbers = [1, 2, 3, 4, 5];
let squares = numbers.map(function(num) {
  return num * num;
});

A named function being used recursively to calculate factorial:

function factorial(n) {
  if (n === 0) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}

Arrow Functions

Arrow functions, introduced in ES6, provide a more concise syntax for writing function expressions. They are anonymous functions and can be named by assigning them to a variable.

Arrow Functions

let greet = () => {
  console.log("Hello, world!");
};

Pros:

  • More concise syntax.
  • Does not have its own this (so neither call, apply and bind methods), arguments, super, or new.target. These are all taken from the outer function when the arrow function is created. This can be a pro when you want this to refer to the outer context.

Cons:

  • Not suitable for object methods because this does not refer to the object itself.
  • Cannot be used as a constructor.
  • They are not hoisted, which means they cannot be called before they are defined.

Anonymous vs Named Functions

Compared to anonymous and named functions, arrow functions have a shorter syntax and do not have their own this value. This makes them well-suited for short, non-method functions, and they are often used in functional programming constructs like map, filter, reduce, etc.

However, because they do not have their own this value, they are not suitable for methods or for functions that will be used as constructors. Also, like anonymous functions, they are not hoisted, so they cannot be called before they are defined.

An arrow function being used in a map operation:

let numbers = [1, 2, 3, 4, 5];
let squares = numbers.map(num => num * num);

A named function being used recursively to calculate factorial:

function factorial(n) {
  if (n === 0) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}

In practice, you'll often see a mix of arrow, named, and anonymous functions in JavaScript code. The choice between them depends on the specific use case.