Skip to content

12 Arrow Functions

Biswajit Sundara edited this page May 1, 2023 · 1 revision

The arrow functions are introduced in ES6 and it's the shorter form of writing functions

  • The syntax of arrow function is hello = () => "Hello World!";
  • The left part denotes the input of a function and the right part the output of that function.

Why arrow functions?

The arrow functions feature allows us to write functions that are less verbose.

Let's say we have a function like this

product = function (a, b) {
  return a * b; 
}

let res = product(2, 3);
console.log(res);

The same can be written using arrow functions. Just drop the keyword functions and introduce => between the parameters and starting curly brace

product = (a,b) =>{
    return a*b;
}

let res = product(2, 3);
console.log(res);

Arrow Function With Multiple Arguments

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

const res = add(4,5);
console.log(res);
we can also write console.log(add(4,5));

Arrow Function With Single Argument

increment = a => a+300;

let sal = increment(200);

console.log(sal);

Arrow Function With No argument

square= () => 2*2

let sq= square();

console.log(sq);

Clone this wiki locally