DEV Community

Cover image for JavaScript Arrow Functions
Krutik Raut
Krutik Raut

Posted on

4 1

JavaScript Arrow Functions

JavaScript Arrow Function
Arrow functions were introduced in ES6.

Arrow functions allow us to write shorter function syntax:

Before Arrow Function

function hello() {
  return "Hello World!";
}
Enter fullscreen mode Exit fullscreen mode

After Arrow Function

let hello =()=> “Hello World”;
Enter fullscreen mode Exit fullscreen mode

Arrow Function Syntax

let myFunction = (arg1, arg2, ...argN) => {
    statement(s)
}
Enter fullscreen mode Exit fullscreen mode

If the body has single statement or expression,
you can write arrow function as:

let myFunction = (arg1, arg2, ...argN) => expression
Enter fullscreen mode Exit fullscreen mode

It gets shorter! If the function has only one statement,
and the statement returns a value, you can remove the brackets
and the return keyword


Arrow Function with No Argument

If a function doesn't take any argument, then you should
use empty parentheses. for example,

let greet = () => console.log('Hello');
greet(); // Hello
Enter fullscreen mode Exit fullscreen mode

Arrow Function with One Argument

If a function has only one argument, you can omit the
parentheses. For example,

let greet = x => console.log(x);
greet('Hello'); // Hello 
Enter fullscreen mode Exit fullscreen mode

Arrow Function with more than one Argument

let x = (x, y) => x * y;
Enter fullscreen mode Exit fullscreen mode

An arrow function expression is a compact alternative to a traditional function expression, but is limited and can't be used in all situations.

Differences & 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.


Thankyou

Follow me on Instagram: https://instagram.com/reboot13_dev

Checkout my Github: https://github.com/reboot13-git

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (1)

Collapse
 
ravindersewach profile image
ravinder sewach

hey krutik raut , can you contact me on telegram id harshsewach. i have some work for you.

thanks

Postmark Image

20% off for developers who'd rather build features than debug email

Stop wrestling with email delivery and get back to the code you love. Postmark handles the complexities of email infrastructure so you can ship your product faster.

Start free

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay