CodeNest

Functions and arrow functions

Two ways to write a function:

function add(a, b) {
  return a + b;
}

const square = (n) => n * n;   // arrow function, implicit return

Arrow functions are compact and common in modern code. Both are called the same way: add(2, 3).

Try it

Implement add and square so the program prints:

5
16
JavaScript in-browser
Next
Loading editor…

Press Ctrl/ + Enter or click Run to see the output here.