JavaScript Arrow Functions


Arrow functions allow a shorter syntax for function expressions. You can skip the function keyword, the return keyword, and the curly brackets. This arrow function does the same thing as a regular function expression. An arrow function uses the => symbol. An arrow function is always written as a function expression.

Syntax

 const add = (a, b) => {
  return a + b;
 };

If the function body contains only one statement, you can remove the word function keyword, the return keyword, and the curly brackets.

 const add = (a, b) => a + b;



   


Conventional Function


Arrow Function




   



   


Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.

     




      “I don’t have a girlfriend,    
      but I know a girl who’d be real mad if she heard me say that.”    
      — Mitch Hedberg