Open In App

Arrow functions in JavaScript

Last Updated : 25 Sep, 2025
Suggest changes
Share
Like Article
Like
Report

Arrow functions in JavaScript are a concise way to write functions using the => syntax, automatically binding this from the surrounding context.

The following code defines an arrow function that takes two numbers and returns their sum.

JavaScript
const add = (a, b) => a + b; console.log(add(5, 3)); 

Output
8 

Syntax:

const functionName = (parameters) => { // function body return result;};

In the above syntax:

  • const: declares the function as a constant variable.
  • functionName: name of the arrow function.
  • (parameters): inputs to the function.
  • =>: arrow showing it’s an arrow function (replaces function keyword).
  • { ... }: function body where code runs.
  • return result; : explicitly returns a value.

1. Arrow Function without Parameters

An arrow function without parameters is defined using empty parentheses (). This type of function is useful when no input values are needed, such as for callbacks, timers, or simple operations.

JavaScript
const gfg = () => {  console.log( "Hi from GeekforGeeks!" ); } gfg(); 

Output
Hi from GeekforGeeks! 

2. Arrow Function with Single Parameters

When an arrow function has only one parameter, parentheses around the parameter can be omitted, making the syntax shorter and cleaner. This is commonly used in callbacks, array methods, or simple operations.

JavaScript
const square = x => x*x; console.log(square(4)); 

Output
16 

3. Arrow Function with Multiple Parameters

Arrow functions with multiple parameters, like (param1, param2) => { }, simplify writing concise function expressions in JavaScript, useful for functions requiring more than one argument.

JavaScript
const gfg = ( x, y, z ) => {  console.log( x + y + z ) } gfg( 10, 20, 30 ); 

Output
60 

4. Arrow Function with Default Parameters

Arrow functions support default parameters, allowing predefined values if no argument is passed, making JavaScript function definitions more flexible and concise.

JavaScript
const gfg = ( x, y, z = 30 ) => {  console.log( x + " " + y + " " + z); } gfg( 10, 20 ); 

Output
10 20 30 

5. Return Object Literals

In JavaScript, returning object literals within functions is concise: () => ({ key: value }) returns an object { key: value }, useful for immediate object creation and returning.

JavaScript
const makePerson = (firstName, lastName) => ({first: firstName, last: lastName}); console.log(makePerson("Pankaj", "Bind")); 

Output
{ first: 'Pankaj', last: 'Bind' } 

Async Arrow Functions

Arrow functions can be made asynchronous by adding the async keyword before the parameter list. This allows the function to use the await keyword inside its body to pause execution until a Promise is resolved, making asynchronous code easier to read and manage.

JavaScript
const fetchData = async () => {  const data = await fetch('https://api.example.com/data');  return data.json(); }; 

In this example:

  • The async keyword marks the function as asynchronous.
  • await can be used inside the function to wait for Promises.
  • Returns a Promise automatically.
  • Useful for handling API calls, timers, or any asynchronous operations.

Arrow Functions vs. Regular Functions

Here is the detailed Comparsion between Arrow Functions and Regular Functions:

AspectArrow FunctionsRegular Functions
this BindingLexically binds this to the surrounding contextthis is dynamically bound depending on how the function is called
SyntaxShorter and more conciseRequires the function keyword
arguments Object

Does not have its own arguments object

Has its own arguments object
Use as ConstructorCannot be used as a constructorCan be used as a constructor with new
Method DefinitionsCannot be used as methods within objectsCan be used to define methods in objects
Return ValueImplicit return for single expressionsMust use return keyword



Explore