Skip to main content

Command Palette

Search for a command to run...

Arrow Functions in JavaScript: A Simpler Way to Write Functions

A beginner-friendly guide to arrow function syntax, implicit return, and how arrow functions differ from normal functions

Updated
5 min read
Arrow Functions in JavaScript: A Simpler Way to Write Functions
P
IT graduate 2024. Learning software and web development in public. Writing about bugs, fixes, small projects, useful tools, and lessons from building things step by step.

Introduction

JavaScript mein jaise-jaise hum modern syntax dekhte hain, ek feature bahut jaldi saamne aata hai:

arrow functions

Arrow functions ko log modern JavaScript style ka common part maante hain.

Aur beginner ke liye ye useful isliye hain kyunki ye function likhne ka shorter aur cleaner tareeqa deti hain.


What Arrow Functions Are

Arrow functions function likhne ka ek modern syntax hai.

Example:

const greet = (name) => {
  return "Hello " + name;
};

Ye bhi function hi hai.

Bas syntax normal function se alag aur shorter hoti hai.


Why Arrow Functions Feel Simpler

Arrow functions boilerplate kam karti hain.

Normal function:

function square(num) {
  return num * num;
}

Arrow function:

const square = (num) => {
  return num * num;
};

Yahan code thoda compact lagta hai.

Isi liye arrow functions modern JavaScript mein commonly use hoti hain.


Basic Arrow Function Syntax

Basic syntax:

const functionName = (parameters) => {
  return value;
};

Ye shape yaad rakhna enough hai beginner start ke liye.


Arrow Function with One Parameter

Example:

const square = (num) => {
  return num * num;
};

Ek aur shorter form bhi possible hai:

const square = num => {
  return num * num;
};

Yani one parameter ke case mein parentheses optional ho sakte hain.


Arrow Function with Multiple Parameters

Jab multiple parameters hon, parentheses clearly use karni hoti hain.

Example:

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

Ye very common pattern hai.


Explicit Return

Jab curly braces use karte ho, toh usually return likhna padta hai.

Example:

const greet = (name) => {
  return "Hello " + name;
};

Isse explicit return kehte hain.


Implicit Return

Arrow functions ka ek useful feature hai implicit return.

Agar body ek simple expression ho, toh return ko skip kiya ja sakta hai.

Example:

const greet = (name) => "Hello " + name;

Ye same tarah kaam kar sakta hai.

Yani:

  • braces ke saath -> explicit return

  • direct expression -> implicit return


Explicit Return vs Implicit Return

Explicit return:

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

Implicit return:

const add = (a, b) => a + b;

Beginner ke liye dono forms dekhna important hai.

Simple rule:

  • body badi hai -> explicit return readable hota hai

  • body chhoti hai -> implicit return clean lag sakta hai


Basic Difference Between Arrow Function and Normal Function

Beginner level par sabse basic difference syntax ka hai.

Normal function:

function add(a, b) {
  return a + b;
}

Arrow function:

const add = (a, b) => a + b;

Arrow functions shorter hoti hain aur modern code mein common hain.

Abhi ke liye hum deep this discussion avoid kar rahe hain, kyunki beginner stage par syntax aur usage samajhna zyada important hai.


Convert Normal Function into Arrow Function

Normal function:

function greet(name) {
  return "Hello " + name;
}

Arrow version:

const greet = (name) => {
  return "Hello " + name;
};

Shorter arrow version:

const greet = (name) => "Hello " + name;

Ye transformation practice beginners ko bahut help karti hai.


A Small Practice Assignment

Try these:

  1. Write a normal function to calculate square of a number

  2. Rewrite it using arrow function

  3. Create an arrow function that returns whether a number is even or odd

  4. Use an arrow function inside map() on an array

Example:

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

Ye modern JavaScript style ka practical use dikhata hai.


Summary

  • arrow functions function likhne ka modern aur shorter syntax hain

  • one parameter aur multiple parameters ke syntax thode different dikhte hain

  • explicit return braces ke saath hota hai

  • implicit return direct expression ke saath hota hai

  • arrow functions normal functions ka compact version feel hoti hain

  • modern JavaScript mein ye bahut common hain


Final Thought

Arrow functions beginners ke liye useful isliye hain kyunki ye JavaScript ko thoda cleaner aur more modern feel karati hain.

Ek baar syntax aur return styles clear ho gaye, toh arrow functions naturally code ka part ban jaati hain.


Continue Reading