BeginnerCoder

Lesson 13 — Function overloads

One name, several signatures

A signature is a function's list of parameter types and its return type. Overloads let one function have several signatures: you write them first without a body, then one implementation whose parameters are broad enough for all of them. Callers only see the overloads, so each call is checked against the shapes you allowed.

function pad(x: string): string;
function pad(x: number): string;
function pad(x: string | number) { return String(x).padStart(3, "0"); }

Why it matters: many library functions accept different inputs; overloads keep each form precise.

Want it another way?
Task

Write the implementation of len below the two overloads so it returns x.length. Expect: 2 3

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.