Arrow Functions Revisited in JavaScript

Arrow Functions Revisited in JavaScript

Arrow Functions Revisited in JavaScript

Arrow functions (=>) in JavaScript provide a shorter syntax and behave differently from traditional function expressions. They are especially useful for callbacks, concise logic, and preserving this.

🔹 Basic Syntax

An arrow function is written as:

const functionName = (param1, param2) => expression;

✅ Example:

const add = (a, b) => a + b; console.log(add(5, 3)); // ✅ Output: 8

✔ No {} or return needed for single expressions.

🔹 Single vs. Multiple Parameters

const greet = name => `Hello, ${name}!`; // ✅ Single parameter, no parentheses needed console.log(greet("Alice")); // Output: Hello, Alice! const multiply = (a, b) => a * b; // ✅ Multiple parameters require parentheses console.log(multiply(2, 4)); // Output: 8

🔹 Multi-Line Functions ({} & return Required)

For functions with multiple statements, use {} and return:

const calculate = (a, b) => { console.log(`Adding ${a} and ${b}`); return a + b; }; console.log(calculate(4, 6)); // Output: 10

🔹 Arrow Functions & this

🚀 Key Difference: Arrow functions do not bind their own this. They inherit this from the surrounding scope.

❌ Regular Function (Loses this)

const user = { name: "Alice", sayHi: function () { setTimeout(function () { console.log(`Hi, ${this.name}`); // ❌ `this` is undefined }, 1000); } }; user.sayHi();

✅ Arrow Function (Preserves this)

const userArrow = { name: "Alice", sayHi: function () { setTimeout(() => { console.log(`Hi, ${this.name}`); // ✅ Works because arrow function doesn't have its own `this` }, 1000); } }; userArrow.sayHi(); // Output: Hi, Alice

✔ Arrow functions inherit this from the surrounding scope.

🔹 Arrow Functions as Callbacks

Arrow functions are perfect for short callback functions.

const numbers = [1, 2, 3, 4]; // ✅ Shorter syntax for `.map()` const squares = numbers.map(n => n * n); console.log(squares); // Output: [1, 4, 9, 16] // ✅ Using `.filter()` const evens = numbers.filter(n => n % 2 === 0); console.log(evens); // Output: [2, 4]

🔹 Arrow Functions & Object Methods

Be careful! Arrow functions don’t have their own this in object methods.

const user = { name: "Alice", greet: () => console.log(`Hello, ${this.name}!`) // ❌ `this` is undefined }; user.greet(); // Output: Hello, undefined!

✔ Use regular functions for object methods instead.

🔹 No arguments in Arrow Functions

Arrow functions don’t have their own arguments object.

function regularFunc() { console.log(arguments); // ✅ Works in regular functions } regularFunc(1, 2, 3); const arrowFunc = () => { console.log(arguments); // ❌ Error: arguments is not defined }; arrowFunc(1, 2, 3);

✔ Use rest parameters (...args) instead:

const arrowWithArgs = (...args) => console.log(args); arrowWithArgs(1, 2, 3); // ✅ Output: [1, 2, 3]

🎯 Summary

Shorter syntax: const func = (args) => expression;
No this binding: Inherits from outer scope.
Great for callbacks: .map(), .filter(), .reduce().
No arguments: Use rest parameters (...args) instead.
Avoid in object methods: Use regular functions instead.

🚀 Arrow functions make JavaScript more concise and readable! Let me know if you need more examples. 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close