Sikau

JavaScript · Day 26 · about 20 min

Arrow functions

A shorter way to write the same thing.

Same recipe, less ink

Modern JavaScript has a shorter way to write functions, using an arrow =>. You'll see it in lots of real code, so let's learn to read and write it.

From function to arrow

function double(n) {
  return n * 2;
}

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

const doubleShort = (n) => n * 2;

console.log(double(4), doubleArrow(4), doubleShort(4));
const greet = () => console.log("Hello!");
const add = (a, b) => a + b;

greet();
console.log(add(3, 4));

The rest of this lesson

  • Reading arrow functionsexplain
  • Predict the outputpredict
  • Read the arrowmatch
  • Your turnwrite code
  • Challengechallenge
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 68 lessons of JavaScript