Junior
Milleks kasutatakse tingimuses noolefunktsiooni?
sobes.tech AI
Vastus AI-lt
Arrow function in a condition is often used for a concise definition of a callback or expression, especially when it is necessary to preserve the this context from the outer scope. Unlike regular functions, arrow functions do not have their own this, which prevents errors when working with object methods.
For example:
const numbers = [1, 2, 3];
const doubled = numbers.map(n => n * 2);
console.log(doubled); // [2, 4, 6]
Here, the arrow function n => n * 2 is used as a short way to describe a function for map. This makes the code more readable and concise.