Category: javascript
Arrow function in javascript
Published on 03 Mar 2026
Explanation
An arrow function is a shorter syntax for
writing functions in JavaScript.
It uses the => symbol.
This example defines a function
that adds two numbers.
Code:
const add = (a, b) => {
return a + b;
};
Explanation
If there is only one parameter and a
single return statement,
parentheses and curly braces can
be omitted.
The value is returned
automatically (implicit
return).
Code:
const square = x => x * x;
Explanation
If there are no parameters,
empty parentheses ()
are required.
This arrow function prints 'Hello'.
Code:
const greet = () => console.log('Hello');
Explanation
Arrow functions do not have their own
'this'.
They inherit 'this' from the
surrounding scope.
So arrowFunc will not refer to the
user object.
Code:
const user = {
name: 'Praveen',
normalFunc: function() {
console.log(this.name);
},
arrowFunc: () => {
console.log(this.name);
}
};
Explanation
Arrow functions are commonly used with
array methods
like map(), filter(), and reduce()
to write cleaner
and shorter callback functions.
Code:
const numbers = [1, 2, 3]; const doubled = numbers.map(num => num * 2); console.log(doubled);