Arrow functions · Funções de seta
Arrow functions
- A shorter way to write a function:
const square = (n) => n * n;. - With a single expression after
=>, the value is returned automatically — noreturn, no{ }.
Arrow functions
- Uma forma mais curta de escrever uma função:
const square = (n) => n * n;. - Com uma única expressão após
=>, o valor é retornado automaticamente — semreturn, sem{ }.
const square = (n) => n * n;
console.log(square(5)); // → 25
More than one parameter
- List the parameters in the brackets, separated by commas.
Mais de um parâmetro
- Liste os parâmetros nos colchetes, separados por vírgulas.
const add = (a, b) => a + b;
console.log(add(3, 4)); // → 7
Watch out
- Need a longer body? Use
{ }and then you must writereturnyourself. - Arrow functions shine as short helpers — you will hand them to
.mapand.filterwhen we reach array methods.
Atenção
- Precisa de um corpo maior? Use
{ }e então você deve escreverreturnsozinho. - Arrow functions brilham como auxiliares curtos — você as passará para
.mape.filterquando chegarmos aos métodos de array.
Common mistakes
x => x + 1is a short function; with no braces it returns the expression.- Arrow functions are handy as arguments to array methods.
Erros comuns
x => x + 1é uma função curta; sem chaves, ela retorna a expressão.- Arrow functions são úteis como argumentos para métodos de array.
Now you try
- Write each arrow function.
- Press Check answer to test it.
Agora você tenta
- Escreva cada arrow function.
- Pressione Check answer para testá-lo.
Write an arrow function triple that returns its argument times 3: const triple = (n) => n * 3;. · Escreva uma função de seta triple que retorne seu argumento vezes 3: const triple = (n) => n * 3;.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Write an arrow function add with two parameters that returns their sum. · Escreva uma função de seta add com dois parâmetros que retorne a soma deles.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Write an arrow function isEven that returns true when its argument is even (n % 2 === 0). · Escreva uma função de seta isEven que retorne true quando seu argumento for par (n % 2 === 0).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.