JavaScript Arrow Functions
How Arrow Function Make Your Code More Ridable More Easy to Write Code

Search for a command to run...
How Arrow Function Make Your Code More Ridable More Easy to Write Code

No comments yet. Be the first to comment.
Node.js doesn't use multiple threads to handle concurrency — and that's not a weakness. Here's exactly how it pulls off handling thousands of connections without breaking a sweat.

Stop confusing params and query in Express.js

Table of Contents What is cURL — in very simple terms Why programmers need cURL Making your first request Understanding request and response Using cURL to talk to APIs Common mistakes beginners

A practical guide to where files live, how to expose them over HTTP, and the security rules you cannot skip.

Understanding authentication approaches · stateful vs stateless · when to use each

XP-VISHAL-WEB-DEV-COHORT-2026-BLOGS
24 posts
Arrow functions are a shorter, more modern way to write functions in JavaScript. Introduced in ES6 (2015), they let you write the same logic with less code — making your programs easier to read and maintain.
💡 Key Idea
Arrow functions don't replace regular functions entirely — they're a cleaner alternative for many common situations, especially short callbacks and one-liners.
The transformation from a regular function to an arrow function follows a simple pattern: remove the function keyword, and add => between the parameters and the body.
// Regular Function
function fnName (params){
const value = params;
return value;
};
// Arrow Function
const arrowFnName = (params){
return value
}
| Regular Function | Arrow Function |
|---|---|
| function greet (){ | |
| return "Hello, Namste!" | |
| }; |
// output = Hello, Namste! | const greet = () => "Hello, Namste From Arrow Function!";
// output = Hello, Namste From Arrow Function! |
When an arrow function has exactly one parameter, you can drop the parentheses around it. This makes the syntax even shorter.
With Parentheses | Without Parentheses |
const double = (n) => { | const double = n => { |
📌 Rule
Only one parameter? You may omit the parentheses. Zero or two+ parameters? Parentheses are required.
When your function takes two or more parameters, always wrap them in parentheses — just like a regular function.
// Two parameters — parentheses required
const add = ( a, b ) {
return a + b;
};
console.log(add(12,6)) // output 18
// Three parameters
const fullName = (first, middle, last) => {
return first + ' ' + middle + ' ' + last;
};
console.log(fullName('Vikas', 'Kumar', 'Singh')); // Vishal Kunar Singh
This is one of the most powerful features of arrow functions. When the function body is a single expression, you can skip both the curly braces {} and the return keyword — the result is returned automatically.
Explicit Return (with curly braces)
const square = (n) => {
return n * n; // 'return' keyword is required with { }
};
Implicit Return (no curly braces)
const square = (n) => n*n // 'return' is implied, no { } needed
⚠️ Important
Implicit return only works for a single expression. If you need multiple statements or lines of logic, always use the explicit form with { } and return.
More Examples
// Greeting — implicit return
const greet = name => 'Hello, ' + name + '!';
console.log(greet('Alice')); // Hello, Alice!
// Check even/odd — implicit return
const isEven = n => n % 2 === 0;
console.log(isEven(4)); // true
console.log(isEven(7)); // false
// Addition — implicit return
const add = (a, b) => a + b;
console.log(add(10, 5)); // 15
Arrow functions and regular functions are mostly interchangeable for simple tasks. The key beginner-friendly differences are about syntax — the deeper differences (like how 'this' works) are for more advanced topics.
Feature | Regular Function | Arrow Function |
Syntax | function name() {} | const name = () => {} |
Shorter syntax | No | Yes |
Implicit return | No | Yes (single expression) |
Hoisting | Yes | No |
'this' binding | Own 'this' | Inherits from parent (advanced) |
Arrow functions shine when used as callbacks inside array methods like map(), filter(), and reduce(). They make the code much easier to read at a glance.
Regular Function with map() | Arrow Function with map() |
const nums = [ 2, 4 , 8, 9 ,18, 45 ]; | const nums = [ 2, 4 , 8, 9 ,18, 45 ]; |
// ── SYNTAX PATTERNS ──────────────────────────────────────────
// No parameters
const sayHi = () => 'Hi!';
// One parameter (parens optional)
const double = n => n * 2;
// Multiple parameters (parens required)
const add = (a, b) => a + b;
// Multiple lines (explicit return required)
const greetFull = (first, last) => {
const name = first + ' ' + last;
return 'Hello, ' + name + '!';
};
// ── WITH ARRAY METHODS ────────────────────────────────────────
const nums = [1, 2, 3, 4, 5];
const doubled = nums.map(n => n * 2); // [2, 4, 6, 8, 10]
const evens = nums.filter(n => n % 2 === 0); // [2, 4]
const sum = nums.reduce((acc, n) => acc + n, 0); // 15