Arrow Function

Sonika Chaudhary
2 min readFeb 26, 2023

An arrow function expression is a compact alternative to a traditional function expression. Instead of the function keyword, it uses an arrow(=>) made up of an equal sign and greater than sign.

Syntax:

() => expression

() => { statements }

(param) => expression

Param => { statements }

(param1, param2) => expression

(param1, param2) => { statements }

· Rest parameters, default parameters and destructuring within params are supported, and always require parentheses.

In a concise body, only a single expression is specified, which becomes the implicit return value. In a block body, you must use an explicit return statement.

Ex-

const isBigger = (a,b) => a>b; (single expression)

console.log(“Is 2 bigger than 3?”, isBigger(2,3));

// Is 2 bigger than 3? False

(Or)

const isBigger2 = (a,b) =>{

return a>b; }; (block body)

Limitations in usage:

· No binding of arguments-

Arrow functions do not have their own arguments object. Thus, in this example, arguments is a reference to the arguments of the enclosing scope

Ex-

const arguments = [1,2,3,4];

const arr = () => arguments[0];

arr(); //- 1

· Line Break before arrow-

An arrow function cannot contain a line break between its parameters and its arrow.

Ex.

const num = (a,b,c)

=>1;

// SyntaxError: Unexpected token ‘=>’

You may put the line break after the arrow –

const num = (a,b,c) =>

1;

In ES6 we use arrow function to save time and code looks concise.

--

--