๐ง JavaScript Functions โ Your Code's Superpower


Functions are the heart of JavaScript. They make code reusable, organized, and logical. Whether you're building a frontend UI or writing backend APIs, functions power everything.
In this blog, weโll explore:
Function declarations vs expressions
Arrow functions
Real-world use cases
Common gotchas
๐ ๏ธ 1. What is a Function?
A function is a block of code designed to perform a task.
function greet() {
console.log("Hello, world!");
}
greet(); // Hello, world!
๐งพ 2. Function Declaration
This is the traditional way to define a function:
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // 5
โ Hoisted โ You can call it before itโs defined in the code.
๐งพ 3. Function Expression
Assign a function to a variable:
const subtract = function (a, b) {
return a - b;
};
console.log(subtract(5, 2)); // 3
โ Not hoisted โ Must be defined before itโs called.
โก 4. Arrow Functions
Introduced in ES6, arrow functions are a concise way to write functions.
const multiply = (a, b) => a * b;
console.log(multiply(3, 4)); // 12
If there's only one parameter and one return line:
const square = x => x * x;
๐ง Arrow Function vs Normal Function
โ Arrow Functions:
Cleaner syntax
Do not bind their own
this
(uses parent scope)
๐ซ Not good for:
Constructors (
new
)When you need your own
this
๐งฉ 5. Real-World Examples
๐งโ๐ป Example 1: Greeting Users in a Web App
function greetUser(name) {
return `Welcome, ${name}!`;
}
console.log(greetUser("Clariofy")); // Welcome, Clariofy!
๐งโ๐ป Example 2: Arrow Function in map()
const prices = [100, 200, 300];
const discounted = prices.map(p => p * 0.9);
console.log(discounted); // [90, 180, 270]
๐งโ๐ป Example 3: Callback Function
function processPayment(amount, callback) {
console.log("Processing:", amount);
callback();
}
processPayment(250, () => {
console.log("Payment Successful!");
});
โ ๏ธ 6. Common Pitfalls
- Forgetting
return
in multi-line arrow functions:
const getName = () => {
name: "Alex";
};
// returns undefined!
// Correct way:
const getNameCorrect = () => {
return { name: "Alex" };
};
- Using
this
inside an arrow function in methods can lead to bugs.
๐ง Summary
Type | Syntax | Hoisted | this Behavior |
Declaration | function foo() {} | โ Yes | Own this |
Expression | const foo = function () {} | โ No | Own this |
Arrow Function | const foo = () => {} | โ No | Inherits this |
๐ฎ Up Next:
Callback, Promise & Async/Await โ mastering async flow in JS!
Subscribe to my newsletter
Read articles from Shaikh Affan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Shaikh Affan
Shaikh Affan
๐ Hey there! Iโm a Frontend Developer & UI/UX Enthusiast from Mumbai, currently pursuing my BSc-IT at Kalsekar Technical Campus. I write about web development, Python, and AI, breaking down complex topics into easy-to-understand articles. My goal is to share knowledge, document my learning journey, and help others grow in tech. ๐ Check out my latest articles and let's learn together!