Functions · Lesson 15 of 45
Arrow functions
A shorter way to write functions, and when not to.
6 minutes4 games
An arrow function is a shorter function: (params) => body. If the body is a single expression, its result is returned with no return or { }. With exactly one parameter the brackets are optional.
JavaScript
const square = n => n * n
const add = (a, b) => a + b
const greet = () => "Hello!"
const describe = (name, age) => {
const group = age >= 18 ? "adult" : "minor"
return `${name} is an ${group}`
}
console.log(square(6), add(2, 3), greet())
console.log(describe("Nour", 20))Quick question
const add = (a, b) => a + b… what does add(2, 3) return?
What's the output?
const f = (x) => { x * 2 }
console.log(f(5))What will this code print?
Order the lines
Order it to print true when the number is above 10.
- Click the lines in the right order
Exercise
Write an arrow function toCelsius converting Fahrenheit: (f - 32) * 5 / 9, then print toCelsius(212).
Expected output: 100
JavaScript
Learn more from
Played 0 of 4 games
Sign in to save your progress and earn XP.