DEV Community

Himanshu Gupta
Himanshu Gupta

Posted on

2

Regular Functions vs Arrow Functions

Arrow functions and regular functions are both ways to define functions in JavaScript, but they have some differences.

Syntax:
Arrow functions have a more concise syntax compared to regular functions. The syntax for arrow functions is as follows:

const arrowFunction = () => {
  // function body
}
Enter fullscreen mode Exit fullscreen mode

The syntax for regular functions is as follows:

function regularFunction() {
  // function body
}
Enter fullscreen mode Exit fullscreen mode

this keyword:
One of the main differences between arrow functions and regular functions is how they handle the this keyword. In regular functions, the value of this is determined by how the function is called. However, in arrow functions, this is lexically scoped, which means that it inherits the value of this from the surrounding code.

arguments keyword:
In regular functions, the arguments keyword can be used to access the arguments passed to the function. However, in arrow functions, the arguments keyword is not available. Instead, you can use the rest parameter syntax to access the arguments:

const arrowFunction = (...args) => {
  // function body
}
Enter fullscreen mode Exit fullscreen mode

Image description

Image description
new keyword:
Regular functions can be used as constructors with the new keyword to create new objects. However, arrow functions cannot be used as constructors.

Overall, arrow functions are a more concise and convenient way to define functions in JavaScript, but they may not be suitable for all situations, especially if you need to use this or the arguments keyword in your function.

Duplicate-named parameters are not allowed

Regular functions example

funcation sumFun(val, val, val){
console.log(val); //3
}

sumFun(1,2,3)


//throw error in strict mode

'use strict';
funcation sumFun(val, val, val){
console.log(val); 
//Uncaught SystaxError:Duplicate Parameter name not allow in this context
}

sumFun(1,2,3)

Enter fullscreen mode Exit fullscreen mode

Arrow functions Example

// SystaxError:Duplicate Parameter name not allow in this context

const sumFun=(val, val, val)=>console.log(val)
Enter fullscreen mode Exit fullscreen mode

No prototype object for Arrow Functions

Image description

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs