Arrow Functions in Javascript

Arrow Functions in Javascript


Besides function declarations and function expressions, JavaScript also has another very concise syntax for defining a function. These functions are called arrow functions.

In this concept, we will focus on the syntax used to write an arrow function. There are differences in the way that an arrow function works, such as this binding.

Here is a comparison between a function declaration and an arrow function.

function addUpTwoNumbers(num1, num2) {
  return num1 + num2;
}

// function keyword removed and => added
const addUpTwoNumbers = (num1, num2) => {
  return num1 + num2;
};
        

Above, you will see that the arrow function syntax:

  1. removes the keyword function
  2. has declared the identifier addUpTwoNumbers as a const
  3. adds a fat arrow =>

If the function body contains only a return statement, like in the example above, the {} and the return keyword can be omitted.

const addUpTwoNumbers = (num1, num2) => { return num1 + num2 };

// can be shortened to
const addUpTwoNumbers = (num1, num2) => num1 + num2;
// braces {} and return removed
        

In the special case of only returning an object from an arrow function, parentheses are needed around the object to be able to omit the return statement.

// explicit return of object
const addUpTwoNumbers = (num1, num2) => {
  return { num1, num2 };
};

// implicit return of object
const addUpTwoNumbers = (num1, num2) => ({ num1, num2 });
        

The use of parenthesis around parameters depends on the number of parameters.

// one parameter does not need parenthesis
const square = num => num * num;

// two or more parameters need to be wrapped in parenthesis
const addUpTwoNumbers = (num1, num2) => num1 + num2;
        

Other concepts such as Rest Parameters and Destructuring can also be used with an arrow function.

Learn More

Muzamil Hussain

ReactJS Developer @Starzplay | Building High-Performance, Scalable Web Applications | 4+ Years in Software Development

6 个月

It is a perfect reminder to refresh my knowledge about Arrow Functions.

要查看或添加评论,请登录

Zeeshan Safdar的更多文章

  • Regular Expressions in JavaScript

    Regular Expressions in JavaScript

    A Regular Expression (or Regex) is a sequence of characters that we can use to target and manipulate certain elements…

    1 条评论
  • Prototypes & Classes in JavaScript

    Prototypes & Classes in JavaScript

    JavaScript includes the capabilities for object-oriented programming (OOP). In OOP, you want to create objects…

  • Strict Mode in React

    Strict Mode in React

    React, being a popular JavaScript library for building user interfaces, offers a powerful feature called to enhance…

  • Sets in JavaScript

    Sets in JavaScript

    In JavaScript, a set is a list-like structure containing unique values, which can be primitives and/or object…

    2 条评论
  • Functions in JavaScript

    Functions in JavaScript

    A function is a block of organized, reusable code that is used to perform some action. There are multiple ways to…

  • Promises in JavaScript

    Promises in JavaScript

    The object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value. The…

  • Array Destructuring | Rest & Spread in JavaScript

    Array Destructuring | Rest & Spread in JavaScript

    About Array Destructuring Array [destructuring assignment][array_destructuring_docs] is a concise way of extracting…

    2 条评论
  • JavaScript Array Transformations

    JavaScript Array Transformations

    In JavaScript, the Array class has many powerful built-in methods for transforming arrays. These methods make it much…

  • Numbers in JavaScript

    Numbers in JavaScript

    There are two different kinds of numbers in JavaScript - numbers and "bigints" Numbers are the most used, and represent…

  • JavaScript Type Conversion

    JavaScript Type Conversion

    In JavaScript, values may be of different types. Changing the type of a variable can be done by explicit type…

    2 条评论

社区洞察

其他会员也浏览了