Arrow Functions in JavaScript: A Simpler Way to Write Functions

If you have been writing JavaScript for a while, you've probably written functions like this:
function greet(name) {
return "Hello, " + name;
}
That works perfectly fine. But JavaScript has a shorter, cleaner way to write functions called arrow functions. They look like this:
const greet = (name) => "Hello, " + name;
That's so much shorter, right? In this blog, we are going to learn everything about arrow functions in intuitive way.
Let's dive in.
What Are Arrow Functions?
Arrow functions are a shorter way to write functions in JavaScript. They were introduced in ES6 (2015) to make code cleaner and easier to read.
Think of them as a "shortcut" for writing functions. Instead of typing function, you use an arrow => (that's why they're called arrow functions).
The main benefits:
Less typing (fewer words to write)
Cleaner, more readable code
Perfect for simple operations
Modern JavaScript style
Now let's see how it works.
Basic Arrow Function Syntax
Let's start by comparing a normal function with an arrow function:
Normal Function:
function sayHello() {
return "Hello!";
}
console.log(sayHello()); // "Hello!"
Arrow Function:
const sayHello = () => {
return "Hello!";
};
console.log(sayHello()); // "Hello!"
What changed?
Removed the word
functionAdded
constand saved it in a variableAdded the arrow
=>after the parentheses
Let's Break It Down:
const functionName = () => {
// Your code here
return something;
};
const functionName- Give your function a name()- Parentheses for parameters (empty if no parameters)=>- The arrow (this is the key part!){}- Curly braces for the function body
Arrow Functions with One Parameter
When your function has only one parameter, you can make it even shorter by removing the parentheses.
Example 1: Greet a Person
Normal function:
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Piyush")); // "Hello, Piyush"
Arrow function (with parentheses):
const greet = (name) => {
return "Hello, " + name;
};
console.log(greet("Piyush")); // "Hello, Piyush"
Arrow function (without parentheses, even shorter!):
const greet = name => {
return "Hello, " + name;
};
console.log(greet("Piyush")); // "Hello, Piyush"
When you have one parameter, you can skip the () around it!
Example 2: Calculate Square of a Number
Normal function:
function square(num) {
return num * num;
}
console.log(square(5)); // 25
console.log(square(8)); // 64
Arrow function:
const square = num => {
return num * num;
};
console.log(square(5)); // 25
console.log(square(8)); // 64
Key rule: One parameter? Parentheses are optional.
Arrow Functions with Multiple Parameters
When you have two or more parameters, you must keep the parentheses.
Example 1: Add Two Numbers
Normal function:
function add(a, b) {
return a + b;
}
console.log(add(5, 3)); // 8
console.log(add(10, 20)); // 30
Arrow function:
const add = (a, b) => {
return a + b;
};
console.log(add(5, 3)); // 8
console.log(add(10, 20)); // 30
Example 2: Create Full Name
Normal function:
function getFullName(firstName, lastName) {
return firstName + " " + lastName;
}
console.log(getFullName("Akash", "Piyush")); // "Akash Piyush"
Arrow function:
const getFullName = (firstName, lastName) => {
return firstName + " " + lastName;
};
console.log(getFullName("Akash", "Piyush")); // "Akash Piyush"
Example 3: Calculate Rectangle Area
Normal function:
function calculateArea(length, width) {
return length * width;
}
console.log(calculateArea(5, 10)); // 50
Arrow function:
const calculateArea = (length, width) => {
return length * width;
};
console.log(calculateArea(5, 10)); // 50
Key rule: Multiple parameters? Keep the parentheses.
Implicit Return vs Explicit Return
This is where arrow functions get really powerful. If your function is simple and just returns one thing, you can make it super short.
Explicit Return (Using return keyword)
This is what we've been doing so far:
const square = num => {
return num * num;
};
You write return explicitly, and you use curly braces {}.
Implicit Return (No return keyword needed!)
If your function body is just one line and you're returning something, you can:
Remove the curly braces
{}Remove the
returnkeywordWrite everything on one line!
const square = num => num * num;
That's it. It automatically returns the result.
Side-by-Side Comparison
Explicit return (longer):
const double = num => {
return num * 2;
};
Implicit return (shorter):
const double = num => num * 2;
Both do the exact same thing. But the second one is cleaner.
When to Use Which?
| Use Explicit Return | Use Implicit Return |
|---|---|
| Multiple lines of code | Single line of code |
| Complex logic | Simple operation |
| Need to do something before returning | Just returning a value |
Example where you MUST use explicit return:
const processNumber = num => {
console.log("Processing: " + num);
let result = num * 2;
console.log("Result: " + result);
return result;
};
You can't make this implicit because there are multiple statements.
Arrow Functions with Array Methods
Arrow functions are super popular with array methods like map(), filter(), etc. They make your code so much cleaner.
Example 1: Using map() to Double Numbers
With normal function:
let numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map(function(num) {
return num * 2;
});
console.log(doubled); // [2, 4, 6, 8, 10]
With arrow function (explicit):
let numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map((num) => {
return num * 2;
});
console.log(doubled); // [2, 4, 6, 8, 10]
With arrow function (implicit - super clean):
let numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6, 8, 10]
Example 2: Using filter() to Get Even Numbers
With normal function:
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let evenNumbers = numbers.filter(function(num) {
return num % 2 === 0;
});
console.log(evenNumbers); // [2, 4, 6, 8, 10]
With arrow function:
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // [2, 4, 6, 8, 10]
Basic Differences: Arrow Functions vs Normal Functions
Let me show you the key differences in a simple way:
1. Syntax Difference
Normal Function:
function add(a, b) {
return a + b;
}
Arrow Function:
const add = (a, b) => a + b;
Arrow functions are shorter!
2. The this Keyword
This is the trickiest part, but I'll keep it simple for now.
Normal functions have their own this.
Arrow functions don't have their own this - they use this from their parent.
const person = {
name: "Piyush",
// Normal function
greetNormal: function() {
console.log("Hello, " + this.name);
},
// Arrow function
greetArrow: () => {
console.log("Hello, " + this.name);
}
};
person.greetNormal(); // "Hello, Piyush" --> Works!
person.greetArrow(); // "Hello, undefined" --> Doesn't work as expected
Simple rule for beginners: Use normal functions for object methods. Use arrow functions for everything else (especially callbacks in map, filter, etc.).
3. Arrow Functions Can't Be Used as Constructors
Normal function (can be a constructor):
function Person(name) {
this.name = name;
}
let person1 = new Person("Piyush"); // Works
console.log(person1.name); // "Ali"
Arrow function (can't be a constructor):
const Person = (name) => {
this.name = name;
};
let person1 = new Person("Piyush"); // Error!
For beginners: Just remember that you can't use new with arrow functions.
4. Implicit Return
Normal function:
function double(num) {
return num * 2; // Must write 'return'
}
Arrow function:
const double = num => num * 2; // No 'return' needed!
When to Use Arrow Functions?
Use arrow functions for:
Short, simple functions
Callbacks in array methods (map, filter, reduce, etc.)
Functions that don't need their own
thisModern, clean code
Use normal functions for:
Object methods
Constructor functions
When you need the
argumentsobjectComplex functions with many lines
Summary
Let's recap what we learned today:
Arrow functions are a shorter way to write functions using =>
One parameter? Parentheses are optional: num => num * 2
Multiple parameters? Keep the parentheses: (a, b) => a + b
Implicit return - No need for return keyword on one-liners
Explicit return - Use {} and return for multiple lines
Perfect for array methods like map, filter, reduce
Main difference: Arrow functions don't have their own this
Arrow functions make your code more readable, modern, and professional. Start using them today, and you'll wonder how you ever lived without them.

