Lesson 5 of 49 beginner

Functions & Arrow Functions

Reusable Recipes of Code

Open interactive version (quiz + challenge)

Real-world analogy

A function is like a vending machine — you press a button (pass arguments), gears turn inside (logic runs), and a snack drops out (return value). Arrow functions are the sleek, touchscreen version of the same machine. Same snacks, less bulk!

What is it?

Functions are reusable blocks of code. They take input (parameters), do work, and return output. TypeScript requires you to specify parameter types and return types. Arrow functions are shorter syntax for the same thing.

Real-world relevance

Almost every function you write will be an arrow function. They're so common in React (hooks, event handlers) that you'll type => a hundred times a day!

Key points

Code example

// Named function — traditional way
function add(a: number, b: number): number {
  return a + b;
}

// Arrow function — full version (pick ONE style)
const addArrow = (a: number, b: number): number => {
  return a + b;
};

// Arrow function — short version (one line!)
const addShort = (a: number, b: number) => a + b;

// No parameters
const sayHi = () => "Hi!";

// One parameter (parentheses optional)
const double = (x: number) => x * 2;

// Multiple lines with arrow function
const greetArrow = (name: string) => {
  console.log("Hello " + name);
  return `Welcome, ${name}!`;
};

// Optional parameter (each example below is standalone)
function greetWithTitle(name: string, title?: string) {
  if (title) {
    return `Hello ${title} ${name}`;
  }
  return `Hello ${name}`;
}
greetWithTitle("John");          // "Hello John"
greetWithTitle("John", "Mr.");   // "Hello Mr. John"

// Default parameter
function greetDefault(name: string, greeting: string = "Hello") {
  return `${greeting} ${name}`;
}
greetDefault("John");           // "Hello John"
greetDefault("John", "Hey");    // "Hey John"

// Rest parameters — any number of arguments!
function sum(...numbers: number[]): number {
  let total = 0;
  for (const n of numbers) {
    total += n;
  }
  return total;
}
sum(1, 2);           // 3
sum(1, 2, 3, 4, 5);  // 15

// Function type
type MathFn = (a: number, b: number) => number;
const add: MathFn = (a, b) => a + b;
const multiply: MathFn = (a, b) => a * b;

// Callback — pass function as parameter
function doMath(a: number, b: number, operation: MathFn): number {
  return operation(a, b);
}
doMath(5, 3, add);       // 8
doMath(5, 3, multiply);  // 15

// Function overloads
function format(value: string): string;
function format(value: number): string;
function format(value: string | number): string {
  if (typeof value === "string") {
    return value.toUpperCase();
  }
  return value.toFixed(2);
}
format("hello");  // "HELLO"
format(3.14159);  // "3.14"

Line-by-line walkthrough

  1. 1. Named function — traditional way
  2. 2. Declaring a function
  3. 3. Returning a value
  4. 4. Closing block
  5. 5.
  6. 6. Arrow function — full version
  7. 7. Declaring a variable
  8. 8. Returning a value
  9. 9. Closing block
  10. 10.
  11. 11. Arrow function — short version (one line!)
  12. 12. Declaring a variable
  13. 13.
  14. 14. No parameters
  15. 15. Declaring a variable
  16. 16.
  17. 17. One parameter (parentheses optional)
  18. 18. Declaring a variable
  19. 19.
  20. 20. Multiple lines with arrow function
  21. 21. Declaring a variable
  22. 22. Printing output to the console
  23. 23. Returning a value
  24. 24. Closing block
  25. 25.
  26. 26. Optional parameter
  27. 27. Declaring a function
  28. 28. Conditional check
  29. 29. Returning a value
  30. 30. Closing block
  31. 31. Returning a value
  32. 32. Closing block
  33. 33.
  34. 34.
  35. 35.
  36. 36. Default parameter
  37. 37. Declaring a function
  38. 38. Returning a value
  39. 39. Closing block
  40. 40.
  41. 41.
  42. 42.
  43. 43. Rest parameters — any number of arguments!
  44. 44. Declaring a function
  45. 45. Declaring a variable
  46. 46. Loop iteration
  47. 47.
  48. 48. Closing block
  49. 49. Returning a value
  50. 50. Closing block
  51. 51.
  52. 52.
  53. 53.
  54. 54. Function type
  55. 55. Defining a type alias
  56. 56. Declaring a variable
  57. 57. Declaring a variable
  58. 58.
  59. 59. Callback — pass function as parameter
  60. 60. Declaring a function
  61. 61. Returning a value
  62. 62. Closing block
  63. 63.
  64. 64.
  65. 65.
  66. 66. Function overloads
  67. 67. Declaring a function
  68. 68. Declaring a function
  69. 69. Declaring a function
  70. 70. Conditional check
  71. 71. Returning a value
  72. 72. Closing block
  73. 73. Returning a value
  74. 74. Closing block
  75. 75.
  76. 76.

Spot the bug

const add = (a: number, b: number): string => {
  return a + b;
};
Need a hint?
Look at what the function actually returns versus its declared return type...
Show answer
The function returns a + b (number + number = number), but the return type is declared as string. Fix: change the return type from 'string' to 'number'.

Explain like I'm 5

A function is like a recipe card. It says: 'Give me flour and eggs, and I'll give you a cake.' You write the recipe once, then use it as many times as you want! Arrow functions are just a shorter way to write the same recipe.

Fun fact

Arrow functions don't have their own 'this' binding. This is confusing at first but makes them perfect for passing to React components!

Hands-on challenge

Write an arrow function that takes an array of numbers and returns the average. Test with [10, 20, 30]!

More resources

Open interactive version (quiz + challenge) ← Back to course: Full-Stack Playbook