...

/

Anonymous vs. Arrow Functions

Anonymous vs. Arrow Functions

Learn the two main differences between the arrow and anonymous functions and choose your style accordingly.

Choosing between arrow and anonymous functions

At first sight, it may appear that arrow functions are direct replacements for anonymous functions. You may be tempted to hastily replace anonymous functions with arrow functions. However, this could result in code that behaves differently than you intend. There are many semantic differences between anonymous functions and arrow functions, and we have to carefully choose between them based on the context.

Learning about the key differences between the two types of functions will help you pick the right one.

In this lesson, we will cover the two main points

  1. Lexical vs dynamic scoping
  2. Differences in bind, call and apply

Lexical vs. dynamic scoping

In any function, most variables either come from the parameters or are defined locally. However, some variables can be defined in an external context.

Example

Let’s consider a short example.

Press + to interact
'use strict';
console.log(
//START:PARAMETER
[1, 2, 3].map(function(e) { return e * 2; })
//END:PARAMETER
);

Explanation

...
Access this course and 1400+ top-rated courses and projects.