Functions in javascript

Functions encapsulate reusable logic. JavaScript supports function declarations, function expressions, and arrow functions. Functions can take parameters, return values, and close over variables from their outer scope. They are first‑class values, so you can pass them around like any other value.

Example

// Function declaration
function add(a, b) {
  return a + b;
}

// Function expression
const subtract = function (a, b) {
  return a - b;
};

// Arrow function
const multiply = (a, b) => a * b;

// Passing functions as values
function calculate(a, b, operation) {
  return operation(a, b);
}

calculate(2, 3, add);       // 5
calculate(5, 1, subtract);  // 4
calculate(2, 3, multiply);  // 6

This shows declaration, expression, and arrow function styles, and how to pass functions into other functions to customize behavior.