{"id":822,"date":"2026-11-17T06:13:12","date_gmt":"2026-11-17T06:13:12","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=822"},"modified":"2026-11-17T06:13:26","modified_gmt":"2026-11-17T06:13:26","slug":"arrow-functions-javascript","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/arrow-functions-javascript\/","title":{"rendered":"Arrow Functions in JavaScript with Examples"},"content":{"rendered":"<p>Hello friends! If you have looked at any modern JavaScript code, you must have seen this little symbol: <code>=&gt;<\/code>. That is an arrow function. Arrow functions, added in ES6 (2015), are a shorter way to write functions in JavaScript. You will see them everywhere in modern code, especially with array methods like <code>map()<\/code> and <code>filter()<\/code>. They are not just shorter, though: they also handle <code>this<\/code> differently from normal functions, which is the part most beginners miss. So let&#8217;s understand both sides, step by step.<\/p>\n<h2>Normal Function vs Arrow Function<\/h2>\n<pre class=\"wp-block-code\"><code>function addNormal(a, b) {\n  return a + b;\n}\n\nconst add = (a, b) =&gt; a + b;\n\nconsole.log(addNormal(2, 3), add(2, 3));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>5 5<\/code><\/pre>\n<p>When the body is a single expression, you can drop the curly braces and the <code>return<\/code> keyword. The value is returned automatically.<\/p>\n<p>That is the basic idea. Now let&#8217;s look at the small rules you need to remember while writing them.<\/p>\n<h2>Arrow Function Syntax Rules<\/h2>\n<pre class=\"wp-block-code\"><code>const square = n =&gt; n * n;          \/\/ one parameter: brackets optional\nconst greet = () =&gt; \"Hello!\";       \/\/ no parameters: empty brackets needed\nconst sum = (a, b) =&gt; {             \/\/ a body with several lines needs { } and return\n  const total = a + b;\n  return total;\n};\nconsole.log(square(4), greet(), sum(5, 6));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>16 Hello! 11<\/code><\/pre>\n<h2>Returning an Object: Wrap It in Brackets<\/h2>\n<pre class=\"wp-block-code\"><code>const makeUser = (name, age) =&gt; ({ name, age });\nconsole.log(makeUser(\"Riya\", 16));\n\nconst broken = (name) =&gt; { name: name };\nconsole.log(broken(\"Riya\"));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>{ name: 'Riya', age: 16 }\nundefined<\/code><\/pre>\n<p>In <code>broken<\/code>, JavaScript reads <code>{ }<\/code> as a function body, not an object, so nothing is returned. Wrapping the object in round brackets fixes it.<\/p>\n<p>So where do arrow functions really shine? With array methods.<\/p>\n<h2>Arrow Functions with Array Methods<\/h2>\n<pre class=\"wp-block-code\"><code>const marks = [72, 45, 88, 91, 60];\nconsole.log(marks.map(m =&gt; m + 5));                 \/\/ add 5 grace marks\nconsole.log(marks.filter(m =&gt; m &gt;= 60));           \/\/ keep 60 and above\nconsole.log(marks.reduce((total, m) =&gt; total + m, 0));  \/\/ total\nconsole.log([...marks].sort((a, b) =&gt; b - a));      \/\/ highest first<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[ 77, 50, 93, 96, 65 ]\n[ 72, 88, 91, 60 ]\n356\n[ 91, 88, 72, 60, 45 ]<\/code><\/pre>\n<p>This is where arrow functions shine: short callbacks that read almost like a sentence.<\/p>\n<p>Friends, up to here arrow functions look like just a shorter way of writing. But now comes the most important part of this lesson.<\/p>\n<h2>The Big Difference: <code>this<\/code><\/h2>\n<p>A normal function gets its own <code>this<\/code>, decided by how it is called. An arrow function does <strong>not<\/strong> have its own <code>this<\/code>; it uses the <code>this<\/code> of the code around it. That makes arrow functions perfect for callbacks inside methods:<\/p>\n<pre class=\"wp-block-code\"><code>const timer = {\n  seconds: 0,\n  startNormal() {\n    setTimeout(function () {\n      console.log(\"normal function this.seconds:\", this.seconds);\n    }, 0);\n  },\n  startArrow() {\n    setTimeout(() =&gt; {\n      this.seconds += 1;\n      console.log(\"arrow function this.seconds:\", this.seconds);\n    }, 0);\n  },\n};\ntimer.startNormal();\ntimer.startArrow();<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>normal function this.seconds: undefined\narrow function this.seconds: 1<\/code><\/pre>\n<p>Inside the normal callback, <code>this<\/code> is no longer <code>timer<\/code>, so <code>this.seconds<\/code> is <code>undefined<\/code>. The arrow callback keeps the <code>this<\/code> of <code>startArrow()<\/code>, which is <code>timer<\/code>.<\/p>\n<h2>When NOT to Use Arrow Functions<\/h2>\n<p>Because of this behaviour, there are a few places where a normal function is the better choice:<\/p>\n<h3>1. As object methods that use <code>this<\/code><\/h3>\n<pre class=\"wp-block-code\"><code>const counter = {\n  count: 5,\n  show: () =&gt; this.count,      \/\/ arrow: this is NOT counter\n  showOk() { return this.count; },\n};\nconsole.log(counter.show());\nconsole.log(counter.showOk());<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>undefined\n5<\/code><\/pre>\n<h3>2. As constructors<\/h3>\n<pre class=\"wp-block-code\"><code>const Person = (name) =&gt; { this.name = name; };\nnew Person(\"A\");<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>TypeError: Person is not a constructor<\/code><\/pre>\n<p>Use a normal function or a <code>class<\/code> instead.<\/p>\n<h3>3. When you need <code>arguments<\/code><\/h3>\n<p>Arrow functions have no <code>arguments<\/code> object. Use rest parameters instead: <code>const count = (...args) =&gt; args.length;<\/code><\/p>\n<h2>Default Parameters Work Too<\/h2>\n<pre class=\"wp-block-code\"><code>const hello = (name = \"Guest\") =&gt; `Hello, ${name}`;\nconsole.log(hello(), hello(\"Aman\"));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Hello, Guest Hello, Aman<\/code><\/pre>\n<p>So friends, to sum up: arrow functions are not faster than normal functions in any meaningful way, so choose them for readability and for how you need <code>this<\/code> to behave. If a method uses <code>this<\/code>, write it with the normal method syntax <code>show() { ... }<\/code> instead. And if your arrow function returns <code>undefined<\/code>, check two things: if you used curly braces, you must write <code>return<\/code>, and if you are returning an object, wrap it in round brackets. Practise with a few array methods and arrow functions will soon feel natural.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/javascript-functions\/\">JavaScript functions<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/function-expression-javascript\/\">function expressions<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/javascript-map-keys\/\">Map keys()<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! If you have looked at any modern JavaScript code, you must have seen this little symbol: =&gt;. That is an arrow function. Arrow functions, added in ES6 (2015), are a shorter way to write functions in JavaScript. You will see them everywhere in modern code, especially with array methods like map() and filter(). &#8230; <a title=\"Arrow Functions in JavaScript with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/arrow-functions-javascript\/\" aria-label=\"Read more about Arrow Functions in JavaScript with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14,15],"class_list":["post-822","post","type-post","status-publish","format-standard","hentry","category-javascript-tutorial","tag-javascript","tag-javascript-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=822"}],"version-history":[{"count":5,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/822\/revisions"}],"predecessor-version":[{"id":6142,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/822\/revisions\/6142"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}