Functions are the beating heart of JavaScript. They are reusable blocks of code built to perform a specific task. We can define them once and run them as many times as we need.

A Simple Analogy: Think of a coffee machine.

A function is exactly like this reusable machine.

🔬 Anatomy of a Function

Before looking at the different types, let's break down the parts of a classic function:

//      (2)Function Name   (3)Parameter
//           |                |
function greet(name) {
  // (4)Function Body: The work to be done
  const message = `Hello, ${name}!`;
  return message; // (5)The return value
}
// (1)Keyword

🗂️ Ways to Define a Function

In modern JavaScript, there are 3 primary ways we define functions:

1. Function Declaration

This is the classic, most fundamental way to define a function.

Syntax:

function calculateSum(a, b) {
  return a + b;
}

// Calling the function
let result = calculateSum(5, 10); // result is 15

2. Function Expression

With this method, we define a function (usually an anonymous one) and assign it to a variable.

Syntax: