DEV Community

Cover image for Arrow functions in JavaScript. How to easily implement them?
Amer Sikira
Amer Sikira

Posted on • Originally published at webinuse.com

Arrow functions in JavaScript. How to easily implement them?

This post was originally published on webinuse.com

Arrow functions were introduced in ES6, as a way to make code more readable and concise. Regular functions can be overkill for some tasks. Arrow functions have a simple syntax, but they also have some limitations.

As per MDN there are differences and limitations:

  • Does not have its own bindings to this or super, and should not be used as methods.
  • Does not have new.target keyword.
  • Not suitable for call, apply and bind methods, which generally rely on establishing a scope.
  • Can not be used as constructors.
  • Can not use yield, within its body.

This is the basic syntax for arrow functions:


const func = (arg, arg2, arg3, arg4, ....., argN) => expression

//If this arrow function was regular function it would look like this

function name(arg, arg2, arg3, arg4, ....., argN) {
    return expression;
}

Enter fullscreen mode Exit fullscreen mode

In the example above we can see the regular arrow function. But there are some differences, more accurately, there can be some differences.

Arrow functions without arguments

If the arrow function has no arguments we just use empty parentheses ().
NOTE By default arrow function assumes return statement if we use a one-liner like in the example above, or below.


const func = () => expression;

//this is equal to

function name () {
    return expression;
}

Enter fullscreen mode Exit fullscreen mode

With one argument

If the arrow function has only one argument, we do not need to use parentheses ().


const func = a => expression;

//this is equal to 
function name(a) {
    return expression;
}

Enter fullscreen mode Exit fullscreen mode

With two or more arguments

Sometimes we have more than one or no argument. In that case we use parentheses ().


const func = (arg, arg2, arg3, arg4, ....., argN) => expression

//this is equal to

function name(arg, arg2, arg3, arg4, ....., argN) {
    return expression;
}

Enter fullscreen mode Exit fullscreen mode

Multiline arrow function

Maybe we need to do something before we return an expression. That would, probably, mean more than one line of function. In that case, we MUST use curly braces {}. Also, when we use curly braces we MUST use return statement because the function does not know what we want to do.


const func = (a,b) => {
    let c = a+b;
    return c;
}

//this is equal to

function name(a,b) {
    let c = a + b;
    return c;
}

Enter fullscreen mode Exit fullscreen mode

When to use arrow functions?

We can use them whenever we "feel like it". There are no strict rules on when (not) to use them, as long as we are aware of implications.

Arrow functions are awesome when we want to have smaller and more readable code. Even though that is not always the case, but 90% of situations it is.

People are using them for example with .map() method.


    const n = [2, 4, 6, 8, 10];
    const r = n.map(item => item * 2);

    console.log(r);
    //Output: [4, 8, 12, 16, 20]
Enter fullscreen mode Exit fullscreen mode

Also, we can use them with the ternary operator.


const button = (loggedIn) ? () => welcome : () => login;

Enter fullscreen mode Exit fullscreen mode

In the example above if a user is logged in then we show a welcome message, otherwise, we show the login button. Assuming that welcome and login are holding such data, respectively.

If you have any questions or anything you can find me on my Twitter, or you can read some of my other articles like JavaScript sort method – part 2.

Top comments (3)

Collapse
 
amersikira profile image
Amer Sikira

Same was with me. When I first saw them, I was like: "Nah, this ain't for me." But after few months you, kinda, naturally start to think this one would be good for arrow function. And you start to use it.

Honestly, I like to use them where they will shorten my code. I don't see a point of using them for anything else. You loose much more than you gain.

Collapse
 
yiannistaos profile image
Yiannis Christodoulou

Thank you, nice article!
Your twitter link doesn’t working.

Collapse
 
amersikira profile image
Amer Sikira

Thank you so much for reading article.

Thank you for pointing out that Twitter link is broken, now I've fixed it.