๐Ÿง  JavaScript Functions โ€“ Your Code's Superpower

Shaikh AffanShaikh Affan
3 min read

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

TypeSyntaxHoistedthis Behavior
Declarationfunction foo() {}โœ… YesOwn this
Expressionconst foo = function () {}โŒ NoOwn this
Arrow Functionconst foo = () => {}โŒ NoInherits this

๐Ÿ”ฎ Up Next:

Callback, Promise & Async/Await โ€” mastering async flow in JS!

0
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!