Arrow Functions
Written By: Avinash Malhotra
Updated on
JavaScript Arrow Function
An Arrow Function in javascript is basically Function Expression, with less code and some limitations. Arrow Functions are primarily used as callback functions in javascript or function Expressions with less code.
Arrow Functions are basically Anonymous Functions used in variables or as callbacks. Arrow Function use Fat Arrows, i.e => to defined a function.
Arrow Function Example
const sayHi = ()=>"hello";
sayHi is variable name with value as arrow function. () are for parameters and "hello" is return value.
How to Create Arrow Functions
Arrow functions can be created using single-line or multi-line syntax.
Single-line Arrow Functions
If the function body is a single expression, you can omit curly braces {} and the return keyword. The value of the expression is returned automatically.
const sayHi = x => `hello ${x}`;
const sayBye = x => `bye bye ${x}`;
const sum = (x, y) => x + y;
Multi-line Arrow Functions
For multiple statements, use curly braces {} and explicitly use the return keyword if you want to return a value.
const sayHi = () => {
return "hello";
};
Arrow Function with Single Parameter
const sayHi = x => {
return `hello ${x}`;
};
Arrow Function with Multiple Parameters
const sum = (x, y) => {
return x + y;
};
Arrow Functions are best recommended for following cases.
- Function expressions (assigning functions to variables)
- Callback functions (e.g., in
map,filter,forEach) - Event handlers (when
thisis not needed) - Short utility functions
Arrow functions are not suitable when you need your function to have its own this, arguments, or to be used as a constructor.
Drawbacks
Arrow functions have some important limitations:
- Do not have their own
thisbinding (they inherit from the parent scope) - Do not have their own
argumentsobject - Cannot be used as constructors (cannot use
newwith them) - Do not have a
prototypeproperty
Arrow Function vs Regular Function
Here is the difference between Arrow Functions and Regular Functions in JavaScript:
| Property | Regular Function | Arrow Function |
|---|---|---|
this keyword | Has its own this | Inherits this from parent |
arguments object | Has its own arguments | No arguments object |
Constructor (new) | Can be used as constructor | Cannot be used as constructor |
prototype property | Has prototype | No prototype |
Example: Arrow Function vs Regular Function
// Regular function
function add(x, y) {
return x + y;
}
// Arrow function
const addArrow = (x, y) => x + y;
console.log(add(2, 3)); // 5
console.log(addArrow(2, 3)); // 5
Arrow functions in Events
Arrow functions can be used in event listeners as callback where this is not required. However we can use event.target where event is parameter of arrow function.
document.querySelector("button").addEventListener("click",(e)=>{
document.querySelector("span").innerHTML="Clicked";
});
Arrow functions in Arrays
Arrow functions are always recommended in arrays as callback for sort, filter other methods. There this keyword in not required.
[1,2,3,4,5,6]
[2,4,6]
const data=[1,2,3,4,5,6];
const even=data.filter(i=>i%2==0);
console.log(data);
console.log(even);
FAQ: JavaScript Arrow Functions
this or arguments.new.this from their parent scope.Summary
JavaScript Arrow Functions provide a concise syntax for writing function expressions and are ideal for callbacks and array methods. However, they have important limitations regarding this, arguments, and cannot be used as constructors. Use arrow functions when you want shorter code and do not need the features of regular functions.