SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
Lesson 26 of 30

JavaScript Arrow Functions

4 min read · Free · No login needed

By now you have written many functions the long way: function add(a, b) { return a + b; }. It works, but it is a lot of typing for a small idea. JavaScript has a shorter way to write the same thing — the arrow function, named after the fat arrow => in the middle. It does the same job in fewer words, and you will see it in almost every modern JavaScript file.

Diagram — see how it fits together
Diagram: JavaScript Arrow Functions

Think of it like a shortcut on your phone. The long way (calling the full number) still works, but the shortcut gets you to the same person faster. Arrow functions are the shortcut: same result, less typing, easier to read once you know the shape.

The shape — three small rules

let add = (a, b) => a + b;      // two params, one line, no return needed
let double = x => x * 2;        // one param — the brackets are optional
let sayHi = () => "Namaste!";   // no params — empty brackets

Rule one: if the body is a single line, you can drop the curly braces — and then the return is automatic. The value of that line is the result. Rule two: with exactly one parameter, the round brackets are optional. Rule three: with no parameters, or more than one, the brackets stay. When the body needs more than one line, you keep the braces and write return yourself, like a normal function.

Note: Arrow functions are perfect for short jobs — a price calculation, a list filter, a button click. For a long function with many steps, the normal function form is easier to read. Short job, short form; long job, long form.

The one big difference — this

Arrow functions are not just shorter. They handle this differently, and this is the part interviewers love to ask about. A normal function gets its own this depending on how it is called — inside a button click, this is the button. An arrow function has no this of its own: it borrows the this from the code around it.

let shop = {
  name: "Chai Shop",
  items: ["Masala", "Adrak", "Kulhad"],
  list: function () {
    this.items.forEach(item => {
      console.log(this.name + " sells " + item);
    });
  }
};

Inside forEach, the arrow function still sees this.name as "Chai Shop", because it borrowed this from list. If you used a normal function there, this would be something else entirely, and this.name would be undefined. That is why arrows are the standard choice inside map, filter and forEach.

Tip: Remember it like this: the fat arrow points at the answer — no return needed for one line. And for this: an arrow borrows, a normal function owns.

Common mistakes

  • Using an arrow function as an object method and expecting this to be the object — it is not. Methods should be normal functions.
  • Writing () => { a + b } and wondering why the answer is undefined — with braces, you must write return yourself.
  • Forgetting the brackets with two parameters — a, b => a + b is a syntax error. It must be (a, b) => a + b.
  • Using arrows everywhere, even for long functions — readability suffers. Arrows are for short jobs.

Remember

  • Arrow syntax: (a, b) => a + b — one line, no braces, no return.
  • One parameter can drop its brackets: x => x * 2.
  • With braces, you must write return yourself.
  • Arrows have no this of their own — they borrow it from the surrounding code.
  • The fat arrow points at the answer — no return needed for one line.

Example explained

  • const price = cup => cup * 10; — an arrow with one parameter, no brackets, one line. price(3) returns 30.
  • const total = (qty, rate) => qty * rate; — two parameters, so the brackets stay. total(3, 10) returns 30.
  • const bill = qty => { let t = qty * 10; return "Total: ₹" + t; }; — with braces the body has two lines, so return is written by hand.
  • document.getElementById("calc").onclick = () => { ... }; — an arrow as a click handler: short, and it borrows this from outside, which is what we want here.
  • document.getElementById("out").textContent = bill(qty); — the arrow's answer is shown on the page.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>Chai Bill — Arrow Functions</title>
<style>
body { font-family: Verdana, sans-serif; background-color: #fff8e1; }
h1 { color: #b45309; text-align: center; }
.card { background-color: white; border: 2px solid #f59e0b; padding: 16px; width: 340px; margin: 20px auto; }
button { background-color: #b45309; color: white; border: none; padding: 10px 18px; font-size: 16px; margin: 4px; }
#out { font-weight: bold; color: green; }
</style>
</head>
<body>

<h1>Chai Bill</h1>
<div class="card">
<p>How many cups? <input id="qty" type="text" value="3"></p>
<p>
<button id="calc">Calculate</button>
<button id="double">Double the order</button>
</p>
<p id="out">Your total will appear here.</p>
<p>Try it: click Calculate for 3 cups (₹30), then Double the order (₹60).</p>
</div>

<script>
const price = cup => cup * 10;
const bill = qty => { let t = qty * 10; return "Total: ₹" + t; };
document.getElementById("calc").onclick = () => {
  let qty = Number(document.getElementById("qty").value);
  document.getElementById("out").textContent = bill(qty);
};
document.getElementById("double").onclick = () => {
  let qty = Number(document.getElementById("qty").value) * 2;
  document.getElementById("out").textContent = bill(qty) + " (doubled!)";
};
</script>

</body>
</html>

Output

Output — how it looks in the browser
Output of JavaScript Arrow Functions

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 30 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

Core Python Programming – 3 Month Complete Course
₹700
View
Android & iOS App Development using Flutter 2026
₹5,000
View
CapCut Mobile Video Editing Course Beginner to Advance
₹99
View
Browse all courses
JavaScript — Lessons
1. JavaScript Introduction 2. JavaScript Where To 3. JavaScript Output 4. JavaScript Syntax 5. JavaScript Comments 6. JavaScript Variables 7. JavaScript Operators 8. JavaScript Data Types 9. JavaScript Functions 10. JavaScript Objects 11. JavaScript Scope 12. JavaScript Events 13. JavaScript Strings 14. JavaScript Numbers 15. JavaScript Arrays 16. JavaScript Array Methods 17. JavaScript Dates 18. JavaScript Math 19. JavaScript Conditions 20. JavaScript Switch 21. JavaScript For Loops 22. JavaScript While Loops 23. JavaScript Type Conversion 24. JavaScript Errors — try, catch, finally 25. JavaScript JSON 26. JavaScript Arrow Functions 27. JavaScript Classes 28. JavaScript Promises and async/await 29. JavaScript and the DOM 30. JavaScript Form Validation
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🛡️ 7-Day Money-Back Guarantee
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Download karne ke liye login karo. Login ke baad yahi page pe rahoge!

Naya ho? Register karo →