DEV Community

Cover image for JavaScript Arrow Functions
Krutik Raut
Krutik Raut

Posted on

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

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