AI Hints
Home›JavaScript Tutorial›Arrow Functions and Scope
JavaScript · Lesson 34 of 138

Arrow Functions

A shorter syntax.

⏱ 2 min read▶ Video at 41:23🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

Modern JavaScript has a shorter way to write functions, using an arrow made of an equals sign and a greater than sign. Arrow functions are everywhere in modern code, so let's learn to read and write them.

Arrow Functions
From the video: Arrow Functions
  • A shorter way
  • An arrow: =>
  • Everywhere in modern code

From long to short

Here's the same function three ways. A normal function. An arrow function stored in a const. And for a single expression, the shortest form, where the braces and the return keyword disappear. With exactly one parameter, you can even drop its brackets. Run it.

app.jsjavascript
function doubleA(n) {
  return n * 2;
}

const doubleB = (n) => {
  return n * 2;
};

const doubleC = n => n * 2;

console.log(doubleA(5), doubleB(5), doubleC(5));
const add = (a, b) => a + b;
console.log(add(2, 3));
Output
10 10 10
5

When to use them

Arrow functions are perfect for short functions, especially when you pass a function to another function, which we'll do a lot with arrays. Regular functions are still fine for bigger, named pieces of work. There's one deeper difference, about the keyword this, which we'll meet with objects.

keyvalue
(a, b) => a + breturns automatically
x => x * 2one parameter
{ return ... }for several lines
Great forshort helpers

Summary

Arrow functions are a short way to write functions. One expression returns automatically. Next, scope, where variables can be seen.

Key takeaways
  • const f = () => {}
  • x => x * 2
  • Short and common
Next up: Scope — where variables live