Understanding Arrow Functions in JavaScript (ES6)

Kevin Diesenberg
2 min readJun 7, 2023

--

In this article, we are going to dig deep into one of the most significant additions in ECMAScript 6 (ES6) — the Arrow Functions. They’ve brought a lot of clarity & code reduction to JavaScript. We’ll start by understanding what Arrow Functions are and then look into their syntax, usage, benefits, and caveats.

What are Arrow Functions?

Arrow Functions, introduced in ES6, offer a new way to declare functions with a shorter syntax. Besides the change in syntax, they also have a unique behavior with the this keyword compared to traditional JavaScript functions.

Syntax

The syntax of an arrow function is its most apparent feature. Let’s take a look:

const traditionalFunction = function(a, b) {
return a + b;
}

const arrowFunction = (a, b) => a + b;

The traditional function is replaced by a constant variable that holds an anonymous function. We no longer use the function keyword. Instead, the arrow (=>) comes after the function parameters.

If the function body just returns a value, we don’t need curly braces {} or a return statement. That's implicit in arrow functions.

this Keyword and Arrow Functions

Arrow functions handle this differently. In traditional functions, this is determined by how a function is called (its execution context). Arrow functions, on the other hand, capture the this value of the enclosing context at the time they are created. Let's look at an example:

const obj = {
traditionalFunc: function() {
console.log(this); // `this` is the `obj` object
},
arrowFunc: () => {
console.log(this); // `this` is NOT the `obj` object, it's the enclosing context (usually `window` in a browser)
}
}

Benefits of Arrow Functions

Shorter Syntax

As we saw earlier, arrow functions can make our code more succinct and easier to read.

No Binding of this

Arrow functions do not bind their own this. This is helpful when you want a function that doesn't have its own this value, but instead uses the this value from its lexical context.

Implicit Returns

If the function body consists of a single statement returning an expression without side effects, it can be written in a simpler form.

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

When Not to Use Arrow Functions

While arrow functions are great, they are not suitable for all situations.

Methods

If you’re creating a method in an object, you’ll probably want to use a traditional function to have the correct this:

const obj = {
value: 'hello world',
sayValue: function() {
console.log(this.value);
}
};

Constructors

Arrow functions can’t be used as constructors. They throw an error when used with new.

Conclusion

Arrow functions are a great addition to JavaScript with ES6. They provide a shorter syntax, do not bind their own this, and offer implicit return, making our code cleaner and more concise. However, they aren't suited for all situations, like methods and constructors, where you'd want to stick with traditional functions. Understanding these differences will help you make the best use of both traditional functions and arrow functions in your JavaScript projects.

Stay tuned for more in-depth explorations of ES6 features!

--

--

Kevin Diesenberg
Kevin Diesenberg

Written by Kevin Diesenberg

Software Engineer | JavaScript | React | Ruby | Rails | SQL | Skydiver | Outdoor Enthusiast | Paramotor Pilot | Cyclist | Edible Mushroom Hunter

No responses yet