SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
JavaScript
1. JavaScript Tutorial for Beginners — Learn JS from Scratch 2. Where to Put JavaScript Code — Head, Body or .js File 3. JavaScript Output Methods — alert, innerHTML, console.log 4. JavaScript Syntax Explained — Statements & Rules 5. JavaScript Comments — How to Write // and /* */ 6. JavaScript Variables — var, let, const Explained 7. JavaScript Operators — Arithmetic, Comparison, Logical 8. JavaScript Data Types — string, number, object, array 9. JavaScript Functions Tutorial — Parameters & Return Values 10. JavaScript Objects — Properties, Methods & this Keyword 11. JavaScript Scope — Global, Local & Block Scope Explained 12. JavaScript Events — addEventListener with Examples 13. JavaScript Strings — Methods, slice, replace & Templates 14. JavaScript Numbers — parseInt, toFixed, NaN Explained 15. JavaScript Arrays — Create, Read, push, pop & More 16. JavaScript Array Methods — map, filter, forEach, find 17. JavaScript Date Object — new Date(), getFullYear & More 18. JavaScript Math Object — random, round, floor, ceil 19. JavaScript if else — Conditions with Examples 20. JavaScript switch Statement — case, break, default 21. JavaScript for Loop — for, for...of, for...in Examples 22. JavaScript while Loop — while & do...while Explained 23. JavaScript Type Conversion — Number, String, Boolean 24. JavaScript try catch — Error Handling with finally 25. JavaScript JSON — stringify & parse with Examples 26. JavaScript Arrow Functions — => Syntax & this 27. JavaScript Classes — Constructor & Objects Explained 28. JavaScript Promises & async/await Explained Simply 29. JavaScript DOM Tutorial — Find & Change Page Elements 30. JavaScript Form Validation Tutorial with Examples 31. JavaScript Fetch API Tutorial — fetch() with Examples 32. JavaScript localStorage — setItem, getItem Explained 33. JavaScript Timers — setTimeout & setInterval Examples 34. JavaScript Regular Expressions — test() & match() Guide 35. JavaScript Debugging — console.log & Fixing Errors
Final Test — Certificate ₹10
Try this code live
Lesson 26 of 35

JavaScript Arrow Functions — => Syntax & this

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 — => Syntax & this

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

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. When can I drop the return in an arrow function?

When the body is a single line without braces — the value of that line is the result automatically.

2. When are brackets optional in an arrow function?

With exactly one parameter, like x => x * 2; with no parameters or more than one, the brackets stay.

3. How does this behave in an arrow function?

An arrow function has no this of its own; it borrows this from the code around it.

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 35 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.

Ethical Hacking & Cyber Security Workshop
View
Android & iOS App Development using Flutter 2026
₹5,000
View
Full Stack PHP Development Course 2026
₹3,000
View
Browse all courses
JavaScript — Lessons
1. JavaScript Tutorial for Beginners — Learn JS from Scratch 2. Where to Put JavaScript Code — Head, Body or .js File 3. JavaScript Output Methods — alert, innerHTML, console.log 4. JavaScript Syntax Explained — Statements & Rules 5. JavaScript Comments — How to Write // and /* */ 6. JavaScript Variables — var, let, const Explained 7. JavaScript Operators — Arithmetic, Comparison, Logical 8. JavaScript Data Types — string, number, object, array 9. JavaScript Functions Tutorial — Parameters & Return Values 10. JavaScript Objects — Properties, Methods & this Keyword 11. JavaScript Scope — Global, Local & Block Scope Explained 12. JavaScript Events — addEventListener with Examples 13. JavaScript Strings — Methods, slice, replace & Templates 14. JavaScript Numbers — parseInt, toFixed, NaN Explained 15. JavaScript Arrays — Create, Read, push, pop & More 16. JavaScript Array Methods — map, filter, forEach, find 17. JavaScript Date Object — new Date(), getFullYear & More 18. JavaScript Math Object — random, round, floor, ceil 19. JavaScript if else — Conditions with Examples 20. JavaScript switch Statement — case, break, default 21. JavaScript for Loop — for, for...of, for...in Examples 22. JavaScript while Loop — while & do...while Explained 23. JavaScript Type Conversion — Number, String, Boolean 24. JavaScript try catch — Error Handling with finally 25. JavaScript JSON — stringify & parse with Examples 26. JavaScript Arrow Functions — => Syntax & this 27. JavaScript Classes — Constructor & Objects Explained 28. JavaScript Promises & async/await Explained Simply 29. JavaScript DOM Tutorial — Find & Change Page Elements 30. JavaScript Form Validation Tutorial with Examples 31. JavaScript Fetch API Tutorial — fetch() with Examples 32. JavaScript localStorage — setItem, getItem Explained 33. JavaScript Timers — setTimeout & setInterval Examples 34. JavaScript Regular Expressions — test() & match() Guide 35. JavaScript Debugging — console.log & Fixing Errors
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...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →