Category: javascript
Arrow functions
Published on 29 Jun 2026
Explanation
Arrow functions provide a shorter syntax for writing functions and were introduced in ES6.
Code:
const greet = () => {
console.log('Hello World');
};
Explanation
Arrow functions with a single parameter do not require parentheses around the parameter.
Code:
const square = x => x * x; console.log(square(5));
Explanation
Arrow functions can have multiple parameters enclosed in parentheses.
Code:
const add = (a, b) => a + b; console.log(add(10, 20));
Explanation
Arrow functions are commonly used as callback functions with array methods like map() and filter().
Code:
const numbers = [1, 2, 3]; const doubled = numbers.map(num => num * 2);
Explanation
Unlike regular functions, arrow functions do not have their own 'this' value and inherit it from the surrounding scope.
Code:
const person = {
name: 'John',
greet() {
setTimeout(() => {
console.log(this.name);
}, 1000);
}
};