CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

JavaScript - Arrow Functions

JavaScript - Arrow Functions

Arrow Functions

Thearrow functionsin JavaScript allow us to create a shorter andanonymousfunction. Arrow functions are written without β€œfunction” keyword. The JavaScriptarrow functionsare introduced in ES6.

Before ES6, we can define a JavaScript function with functiondeclarationor functionexpression. The function expressions are used to define anonymous functions. The arrow functions allow us to write the function expressions with shorter syntax.

Let’s look at the below syntax to write a function expression βˆ’

JAVASCRIPT CODE
const varName = function(parameters) {
    // function body
};

The above function expression can be written as an arrow function βˆ’

JAVASCRIPT CODE
const varName = (parameters) => {
    // function body
};

Here the β€œfunction” keyword is removed and after parenthesis we added β€œ=>β€œ.

Syntax

The syntax to use the arrow function in JavaScript is as follows.

JAVASCRIPT CODE
const varName = (p1, p2, ... pN) => Expression;
OR
const varName = (p1, p2, ...pN) => {
    // function body
};

Here the parameters p1, p2, , pN are optional. We can use the variable name followed by pair of parentheses to invoke the arrow function.

Arrow Function with Single Statement

When the arrow function contains asingle statement, we don’t need to write the β€˜return’ keyword and braces (curly brackets).

JAVASCRIPT CODE
const add = (x, y) => x +y;

Please note, we can always write an arrow function with return keyword and braces.

JAVASCRIPT CODE
const add = (x, y) => {return x + y};

Example

In the example below, the arrow function contains a single statement, so we don’t need to use the curly braces or return statement.

Preview

Output

JAVASCRIPT CODE
2

Arrow Function with Multiple Statements

When the function body containsmultiple statements, we should always use the β€˜return’ statement to return a value. Also we should use the curly brackets.

Example

In the example below, the arrow function contains multiple statements, so we need to use thebracesorreturnstatement.

Preview

Output

JAVASCRIPT CODE
2

Note when we use block body using braces, we must use return statement.

Arrow Functions Without Parameters

The parameters p1, p2, , pN, in the above syntaxes are options. We can write an arrow function without any parameters.

JAVASCRIPT CODE
const greet = () => "Hello World!";

We can also write using block body using braces and return keyword βˆ’

JAVASCRIPT CODE
const greet = () => {return "Hello World!";};

Example

Preview

Output

JAVASCRIPT CODE
Hello World!

Arrow Function with Parameters

Example: Arrow function with a single parameter

The below code demonstrates that when you need to pass a single parameter to the function, you don’t need to write parameters in the pair of parenthesis.

Preview

Output

JAVASCRIPT CODE
The value returned from the arrow function is: 10

Example: Arrow function with multiple parameters

We pass the multiple parameters to the arrow function expression in the code below. When the body of the arrow function contains multiple statements, we need to write it inside the curly braces and use the return statement to return the value.

Preview

Output

JAVASCRIPT CODE
The sum of 10, 30, 45, and 60 is: 145

Arrow Function as an Expression

The arrow function can easily be used as an expression due to its shorter syntax.

Example

In the below code, we use the ternary operator, and based on the boolean value of the β€˜isMul’ variable, we assign the arrow function expression to the β€˜func’ variable.

After that, we use the β€˜func’ variable to call the arrow function stored in that.

Preview

Output

JAVASCRIPT CODE
The multiplication value is: 25

Arrow Function with Default Parameters

Example

The below code explains how programmers can pass the default parameters to the arrow function. It is similar as we pass default parameters to the standard function definition.

Preview

Output

JAVASCRIPT CODE
mul(5, 8) = 40
mul(6) = 90
mul() = 150

Benefits of Using Arrow Functions

Here, we have explained the benefits of using the arrow functions.

  • Shorter syntaxβˆ’ The arrow function decreases the code size to define the function.
  • Implicit returnβˆ’ To return the resultant value of the expression from the arrow function containing only a single statement, developers don’t need to use the return keyword.
  • Ease to use as expressionβˆ’ The arrow function can be easily used as an expression.

Limitations of Using Arrow Function

There are some limitations of arrow functions, which we have explained below.

  • No Argumentsβˆ’ The arrow function can’t have an arguments object.
  • No prototypeβˆ’ The arrow function can’t have a prototype property as it is stored in the variable as an expression.
  • No new keywordβˆ’ The arrow function can’t be used with the new keyword to create its object.