JavaScript – Arrow Functions

The arrow function was first introduced in ECMAScript 6 (ES6). It provides a concise and shorthand for declaring an anonymous function as compared to regular function.

Arrow functions in JavaScript are used in callback chains, promise chains, array methods.

Why to use Arrow function?

  • can be declared in just one line of code.
  • No function keyword required.
  • No return keyword required.
  • No curly braces {} required.

To understand clearly, let us see an example given below-

Using Normal Function:

function hello()
{
    document.write('Welcome to Advance JavaScript Tutorial!');
}
hello();

#Output
Welcome to Advance JavaScript Tutorial!

Before ES6 Version, we use this way to short our JavaScript syntax

let hello = function() {
    document.write("Welcome to Advance JavaScript Tutorial!");
}

hello();

#Output
Welcome to Advance JavaScript Tutorial!

But using Arrow function, we can get more concise JavaScript syntax

let hello = () => document.write("Welcome to the Advance JavaScipt Tutorial!");
hello();

#Output
Welcome to the Advance JavaScipt Tutorial!

Arrow function with parameters

let hello = (name,age) => document.write(`Hello ${name}, Are you become ${age} years old?`);
hello('Suman', 29);

#Output
Hello Suman, Are you become 29 years old?
Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial