DEV Community

loading...
Cover image for Enough JavaScript to get you Started : #15 Arrow Functions

Enough JavaScript to get you Started : #15 Arrow Functions

whoadarshpandya profile image Adarsh Pandya ・3 min read

Arrow Functions?

πŸ‘‰ Since we've covered Basics of JavaScript , it's now time to move towards slightly advance things πŸŽ‰


πŸ‘‰Arrow Functions are compact alternatives to regular JavaScript functions


πŸ‘‰Arrow Functions will help you jump into modern JS concepts , and extremely helpful before learning any JS frameworks like React or Angular

Have you seen something like this?

πŸ‘‰ Sometimes when our code doesn't work as expected so we go to stack overflow or GitHub for finding solutions.

πŸ‘‰ Let's say you were getting error in Adding 2 numbers (i know you won'tπŸ˜‚, but still)...

πŸ‘‰ You go to stack overflow and see something like this...

var sum = (a,b) => {
      return a+b;  
}
Enter fullscreen mode Exit fullscreen mode

WTF was that? Language of gods? πŸ€”

πŸ‘‰ Let's break down this into simple function


πŸ‘‰ So what's written here basically, is this...


function sum(a,b)
{
       return a+b;
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Arrow function helps to reduce syntax (often known as syntactic sugar) over vanilla js by ES2016


Benefits of Array Functions

πŸ‘‰ Helps to reduce syntax

πŸ‘‰ Arrow syntax automatically binds this to the surrounding code’s context (will be covered later in series 😁)

πŸ‘‰ Implicit Return

What are Arrow Functions any way?

πŸ‘‰ Arrow function is modern way of writing normal JavaScript function which helps us in writing lesser and more meaningfulfunctions.


πŸ‘‰ Arrow Functions are also known as Fat Arrow Functions because of the => expression in syntax


πŸ‘‰ Syntax

// normal function
var functionName = function (param1,param2) {
      // function body
      // ...
      // ...
}

// arrow function
var functionName = (param1,param2) => {
       // function body
       // ...
       // ...
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ in simple terminology , we assign an anonymous function to a variable where the name of variable becomes the name of function and can be called by functionName(); .

πŸ‘‰ Parentheses in arrow function indicates number or params our function will take (in our case there are two , namely param1 and param2)


πŸ‘‰ After Arrow (=>) , the curly braces indicates the start of function body , where we can write whatever we want inside it.

Examples

πŸ‘‰ Writing a Arrow Function which can return sum of 2 nums

// sum : name of arrow function
// n1,n2 : params of arrow function
// {...} : body of arrow function

var sum = (n1,n2) => {
      return n1+n2;
}

// can be called like : 
sum(1,2);
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Writing a Arrow Function which can return cube of given number


// arrow function with one param
var cube = num => {
       return num*num;
}

// can be called like : 
cube(2);
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Writing a Arrow Function which can will ask for user age and checks if he/she is eligible for license

// arrow function with 0 params
var licenseValidator = () => {
    var age = +prompt("what's your age ? ");
    if(age > 18)
    {
          console.log("eligilbe")
    } else {
          console.log("not eligible"); 
    }
}
Enter fullscreen mode Exit fullscreen mode

Rules for Writing Arrow functions

πŸ‘‰ => is compulsory before function body.

πŸ‘‰ we've to pass empty parentheses() compulsory if function takes 0 params. (otherwise we'll end up getting error πŸ˜‚).

πŸ‘‰ If Arrow function takes single param , then the parentheses are optional.

Bonus🎁 : Concise arrow functions

πŸ‘‰ Concise arrow functions are more optimized than normal arrow functions

πŸ‘‰ Concise arrow functions can be used where we've only return expression in our function body (no {} curly parentheses)

πŸ‘‰ No need to write return explicitly in concise arrow functions

πŸ‘‰ Not every arrow function can be converted to Concise arrow function

πŸ‘‰ *Example : *

// making sum arrow function concise 
var sum = (num1,num2) => num1+num2;

sum(1,3); // returns 4

var cube = num1 => num1*num2;

cube(2); // returns 4

Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Notice we haven't converted licenseValidator to concise arrow function because that function has some amount of calculations inside it's body.

πŸ‘‰ That's what i meant when i wrote 'not all arrow functions can be converted to concise arrow functions'

Let me know in comment section if you have any doubt or feedback. it's always worth to give time to the thriving developer community :)

Keep Coding ❀

Hey , Let' ConnectπŸ‘‹

Twitter / Github

Discussion (0)

pic
Editor guide