Javascript Arrow Function

Javascript Arrow Function

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!";
}

After Arrow Function

let hello =()=> “Hello World”;

Arrow Function Syntax

let myFunction = (arg1, arg2, ...argN) => {
    statement(s)
}

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

let myFunction = (arg1, arg2, ...argN) => expression

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

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

Arrow Function with more than one Argument

let x = (x, y) => x * y;

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 bindings to this or super, and should not be used as a method.

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

Twitter: twitter.com/reboot13_dev

Instagram: instagram.com/reboot13_dev

Github: github.com/reboot13-git

Did you find this article valuable?

Support reboot13 by becoming a sponsor. Any amount is appreciated!