JavaScript Functions

Introduction to Functions

Functions are reusable blocks of code that perform specific tasks. They can take inputs (parameters) and return outputs. Here's an example:

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

console.log(add(2, 3)); // 5

Function Declarations

Define named functions using the function keyword.

Function Expressions

Create anonymous functions assigned to variables.

Arrow Functions

Arrow functions provide a concise syntax for writing functions. They also have a lexical this binding, which means they inherit this from their surrounding context:

const multiply = (a, b) => a * b;

console.log(multiply(4, 5)); // 20