Category: javascript
Arrow Function
Published on 18 Feb 2026
Explanation
Arrow function is a shorter syntax for
writing
functions in JavaScript. It was introduced in ES6
and uses the => symbol.
Code:
const greet = () => {
console.log("Hello World");
};
greet();
Explanation
Arrow functions can take parameters. If
there is
only one parameter, parentheses are optional.
Code:
const square = x => x * x; console.log(square(5));
Explanation
When there are multiple parameters,
parentheses are required
around them.
Code:
const add = (a, b) => a + b; console.log(add(10, 20));
Explanation
Arrow functions can return values directly
without using
the return keyword if the function has a
single expression.
Code:
const multiply = (a, b) => a * b; console.log(multiply(3, 4));
Explanation
If you use curly braces, you must
explicitly
use the return keyword.
Code:
const subtract = (a, b) => {
return a - b;
};
console.log(subtract(10, 5));
Explanation
Arrow functions do not have their own this.
They inherit this from the surrounding lexical scope.
Code:
function Person() {
this.age = 0;
setInterval(() => {
this.age++;
console.log(this.age);
}, 1000);
}
new Person();