Hackforge Academy

Category: javascript

Arrow function in javascript

Published on 03 Mar 2026

Explanation

An arrow function is a shorter syntax for writing functions in JavaScript.
It uses the => symbol. This example defines a function that adds two numbers.

Code:

const add = (a, b) => {
  return a + b;
};

Explanation

If there is only one parameter and a single return statement, parentheses and curly braces can be omitted. The value is returned automatically (implicit return).

Code:

const square = x => x * x;

Explanation

If there are no parameters, empty parentheses () are required. This arrow function prints 'Hello'.

Code:

const greet = () => console.log('Hello');

Explanation

Arrow functions do not have their own 'this'. They inherit 'this' from the surrounding scope. So arrowFunc will not refer to the user object.

Code:

const user = {
  name: 'Praveen',
  normalFunc: function() {
    console.log(this.name);
  },
  arrowFunc: () => {
    console.log(this.name);
  }
};

Explanation

Arrow functions are commonly used with array methods like map(), filter(), and reduce() to write cleaner and shorter callback functions.

Code:

const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled);

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community