{"id":380,"date":"2023-11-13T07:38:03","date_gmt":"2023-11-13T07:38:03","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=380"},"modified":"2023-11-17T06:29:27","modified_gmt":"2023-11-17T06:29:27","slug":"arrow-function-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/arrow-function-javascript\/","title":{"rendered":"JavaScript Arrow Function (Introduction with Examples)"},"content":{"rendered":"\n<p>In the world of JavaScript, developers are constantly looking for ways to write more concise and readable code. One powerful tool that has emerged in recent years is the arrow function. In this introduction guide, we will take a deep dive into the Arrow Function in JavaScript with some examples, its use cases, advantages, and limitations. <\/p>\n\n\n\n<p>Before that, let&#8217;s revise the concept of function!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are the Functions in JavaScript?<\/strong><\/h2>\n\n\n\n<p><strong>A function is a block or collection of statements or code used to implement a particular task.<\/strong> The syntax of writing a function in JavaScript is as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>function name_of_function (parameter1, parameter2,....){\n    \/\/code to be executed\n    \/\/return statement&nbsp;\n}<\/code><\/pre>\n\n\n\n<p>Let us take the example of printing numbers up to three and four without using a function first.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/printing numbers upto 3\nfor(let i=1;i&lt;=3;i++){\n\u00a0\u00a0\u00a0\u00a0console.log(i);\n}\n\n\/\/printing numbers upto 4\nfor(let i=1;i&lt;=4;i++){\n\u00a0\u00a0\u00a0\u00a0console.log(i);\n}<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>1\n2\n3\n1\n2\n3\n4\n<\/code><\/pre>\n\n\n\n<p>Now, let us perform and get the same output with the help of a function. We will see that the amount of code has reduced significantly. Instead of using two for-loops, we will get the work done by using a single for-loop in the function and passing parameters.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring and defining a function used to print numbers from 1 to n\nfunction printnumbers(n){\n    for(let i=1;i&lt;=n;i++){\n        console.log(i);\n    }\n}\n\n\/\/calling the function\n\/\/parameter n=3\nprintnumbers(3);\n\n\/\/parameter n=4\nprintnumbers(4);<\/pre><\/div>\n\n\n\n<p>It is time to look into a concise and compact way of writing functions in JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are the Arrow Functions in JavaScript?<\/strong><\/h2>\n\n\n\n<p><strong>Arrow functions are a concise alternative to traditional function expressions in JavaScript. They are bound by any name or by the keyword function, they are anonymous functions.<\/strong> It makes the code more readable. It is also known by the name of Lambda Functions.<\/p>\n\n\n\n<p>The syntax of the arrow function is as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>const functionName=(parameter1, parameter2,...)=&gt;{ \/\/code to be executed }<\/code><\/pre>\n\n\n\n<p>An example of the it is given below:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring arrow function\nconst greet=()=&gt;{console.log(&quot;Hello, FavTutor!&quot;)};\n\n\/\/calling the arrow function\ngreet()<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Hello, Favtutor!<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Examples of Arrow Functions<\/strong><\/h2>\n\n\n\n<p>Let\u2019s discuss three different scenarios of using it:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>With No Parameter<\/strong><\/h3>\n\n\n\n<p>There are no parameters given in the brackets. Let\u2019s see how this function performs when executed.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring arrow function with no parameters\nconst greet=()=&gt;{\n    console.log(&quot;Hello, FavTutor!&quot;)\n};\n\n\/\/calling the arrow function\ngreet()<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Hello World<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>With Some Parameters<\/strong><\/h3>\n\n\n\n<p>We declare a function by providing parameters a, b, and c. The parameters are specified when we call a function. The function returns an expression after evaluation.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring arrow function with parameters\nconst solve=(a,b,c)=&gt;{\n    return a+b*c;\n};\n\n\/\/calling it and printing the Output\nconsole.log(solve(1,2,3));<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>7<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>With Default Parameters<\/strong><\/h3>\n\n\n\n<p>We declare a function by providing parameters a, b, and c with a default value. The function returns an expression after evaluation.&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring arrow function with parameters\nconst solve=(a=1,b=2,c=3)=&gt;{\n    return a+b*c;\n};\n\n\/\/calling it and printing the Output\nconsole.log(solve());<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>7<\/code><\/pre>\n\n\n\n<p>In this example, we have specified the default parameters, a as 1, b as 2, and c as 3. We receive the desired output.<\/p>\n\n\n\n<p>What if we provide the parameters in the function while calling the function, even though the default parameters are already specified? The function will take the final parameters as those that are used while calling the function.<\/p>\n\n\n\n<p>Let us take an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring arrow function with parameters\nconst solve=(a=1,b=2,c=3)=&gt;{\n    return a+b*c;\n};\n\n\/\/calling it and printing the Output\nconsole.log(solve(4,5,6));<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>34<\/code><\/pre>\n\n\n\n<p>In this example, we have used default parameters along with providing different parameters when we call the function. As observed, the function takes the final parameters. We obtain the desired result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Advantages of Arrow Functions<\/strong><\/h2>\n\n\n\n<p>The advantages of using Arrow Functions are discussed below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Compared to the Regular Functions, the arrow functions provide a compact, concise, and easy-to-understand method of writing functions.<br><\/li>\n\n\n\n<li><strong>The return statement in the Arrow Function is Implicit when we use a single expression in the function body.<\/strong> Thereafter, the need for curly braces becomes as low as zero.<br><\/li>\n\n\n\n<li>It is so advanced that it is able to automatically capture the \u201cthis\u201d value from the surrounding context. This eliminates the need to bind, call, or apply methods to maintain the correct \u201cthis\u201d reference.<br><\/li>\n\n\n\n<li>It is particularly useful when working with callbacks or event handlers because of their expressive way of defining these functions.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Disadvantages\/Limitations<\/strong><\/h2>\n\n\n\n<p>The disadvantages of this unique function are discussed below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Arrow Functions can not be used with the \u201cnew\u201d keyword. Attempting to do so will result in a TypeError.<br><\/li>\n\n\n\n<li>It can not be used as Constructors.<br><\/li>\n\n\n\n<li>It does not have its own arguments or object. Instead, they inherit the arguments from the surrounding scope.<br><\/li>\n\n\n\n<li>In arrow functions, this is lexically bound to the surrounding scope, which means it cannot be dynamically changed. This can be problematic when you need to access different contexts within a function.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Overall, Arrow functions are a powerful addition to JavaScript, providing a more concise and expressive way of writing functions. We dwelled further into various examples of using it in different scenarios.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the world of JavaScript, developers are constantly looking for ways to write more concise and readable code. One powerful tool that has emerged in recent years is the arrow function. In this introduction guide, we will take a deep dive into the Arrow Function in JavaScript with some examples, its use cases, advantages, and [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":382,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11,34],"class_list":["post-380","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-javascript-functions"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=380"}],"version-history":[{"count":5,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/380\/revisions"}],"predecessor-version":[{"id":488,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/380\/revisions\/488"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/382"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}