CricketScript Lesson 7 of 11 · Helmet on?  |  Course

Helmet on?

Some parameters are required. Some are optional. Some have a default.

Walking to the crease against a 145 km/h fast bowler, the helmet is on — non-negotiable. Facing a part-time spinner in a knock-around match, you might leave it in the bag. The function that pads up a batter cares about name, but the helmet is optional.

TypeScript marks an optional parameter with a question mark: helmet?: boolean. Better still, give it a default: helmet: boolean = true. Now callers can skip it entirely — and TypeScript fills in the safe choice.

Toggle the helmet. Call padUp with and without it. Watch the function fill in the gap when you skip the second argument.

Toggle the helmet on or off — or skip the argument entirely.
AS
Aarav Sharma
Opener · Right-hand

Optional with a default — the helmet is on, unless you say otherwise.

The ? makes a parameter optional. An = gives it a default. Together, helmet: boolean = true says: you may leave it out — and if you do, assume the safe choice.

// 1. Optional — the ? lets the caller skip it.
function padUp(name: string, helmet?: boolean): string { /* ... */ }

// 2. Optional with a default — assume the safe choice.
function padUp(name: string, helmet: boolean = true): string {
  return helmet
    ? `${name} pads up, helmet on.`
    : `${name} pads up, cap only.`;
}

padUp('Sharma', true);   // 'Sharma pads up, helmet on.'
padUp('Sharma', false);  // 'Sharma pads up, cap only.'
padUp('Sharma');          // 'Sharma pads up, helmet on.'  // default kicks in

Required, optional, default — three shapes of an argument.

You called padUp three ways. With true, with false, and with the second argument omitted entirely. The third call worked because the default — helmet = true — stepped in for the missing value.

Use ? when a parameter is genuinely optional and the function can cope without it. Use a default when there's a safe choice the caller usually wants anyway — like a helmet.

Reading a paid lesson? The rest is right here.

This is one of 9 paid lessons. Lessons 1 and 9 are free previews if you want to sample first — otherwise the full course unlocks in one click.

$15worldwide · ₹625India (50% off)
Unlock all 11 lessons + bonus →
30-day money-back guarantee · Try Lessons 1 and 9 free first if you'd rather
Made by one person, not a company. Questions: [email protected]