Arrow Function

Arrow functions allow a short syntax for writing function expressions. You don't need the function keyword, the return keyword, and the curly brackets.

const x = (x, y) => { return 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:

Use of this keyword

Unlike regular functions, arrow functions do not have their own this. For example:

let user = { 
    name: "GFG", 
    gfg1:() => { 
        console.log("hello " + this.name); // no 'this' binding here 
    }, 
    gfg2(){        
        console.log("Welcome to " + this.name); // 'this' binding works here 
    }   
 }; 
user.gfg1(); 
user.gfg2(); 

Availability of arguments objects

Arguments objects are not available in arrow functions, but are available in regular functions.

Example using regular()

let user = {       
    show(){ 
        console.log(arguments); 
    } 
}; 
user.show(1, 2, 3);

Example using arrow()

let user = {      
        show_ar : () => { 
        console.log(...arguments); 
    } 
}; 
user.show_ar(1, 2, 3)

Last updated