Regular Functions vs Arrow Functions
Himanshu Gupta
Posted on March 17, 2023
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
}
The syntax for regular functions is as follows:
function regularFunction() {
// function body
}
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
}
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)
Arrow functions Example
// SystaxError:Duplicate Parameter name not allow in this context
const sumFun=(val, val, val)=>console.log(val)
No prototype object for Arrow Functions
Posted on March 17, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
January 23, 2024