liveBook · Manning

Default value

As explained in Essential TypeScript 5, Third Edition

A default parameter is a function parameter with an initializer used when the caller omits the argument or supplies undefined.

A default parameter supplies a fallback when a function call omits an argument or passes `undefined`. Write the initializer after the parameter name: `discount = 0`. The parameter can then be omitted without requiring a check for `undefined` inside the function. section 8.2.2

Calculate tax with an optional discount

In `src/index.ts`, `function calculateTax(amount: number, discount: number = 0) { return (amount * 1.2) - discount; }` accepts both `calculateTax(100, 0)` and `calculateTax(100)`. Both return `120`. Changing the fallback in the declaration changes it wherever the function uses `discount`. section 8.2.2

A default-initialized parameter is optional, but it has no `?` marker and must follow required parameters. It handles a missing argument, not arbitrary extra arguments; use a rest parameter when a function needs to collect additional values. section 8.2.2

A fallback does not fix unrelated type problems. In JavaScript, `sumPrices(first, second, third = 0)` returns `300` for two numeric arguments, avoiding the earlier `NaN` result. If an argument is the string `"100"`, addition still concatenates: the demonstrated result is `"1001000"`. section 3.3.3

Listing 8.8 Using a default parameter value in the index.ts file in the src folder
function calculateTax(amount, discount = 0) {
    return (amount * 1.2) - discount;
}

let taxValue = calculateTax(100, 0);
console.log(`2 args: ${taxValue}`);
taxValue = calculateTax(100);
console.log(`1 arg: ${taxValue}`);
//taxValue = calculateTax(100, 10, 20); 
//console.log(`3 args: ${taxValue}`);

See the complete `src/index.ts` tax example with the defaulted discount and calls using one or two arguments. section 8.2.2

The same pattern works in application functions: `applyDiscount(product, discount = 5)` gives callers a standard discount while allowing them to supply another value. section 4.5.5

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Works through syntax, calls, outputs, and parameter-order constraints in JavaScript and TypeScript, without detailing initializer evaluation order.
Examples
Uses a tax calculation, a price-summing function, and a product-discount function to show fallback values in calls.
What it assumes
Assumes familiarity with JavaScript functions and calls while explaining TypeScript’s `?` and `=` parameter forms.
What you can do after
Practice/perform defining fallback parameters in JavaScript or TypeScript functions and checking that the fallback does not mask other argument problems.
How it connects
Connects default-initialized parameters to optional parameters, rest parameters, type annotations, and JavaScript string concatenation.

Also explained in

Compare how 20 books explain default value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.