Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Default Parameters | Advanced Parameters and Callbacks
TypeScript Functions and Parameters

bookDefault Parameters

Default parameters let you provide a value for a function parameter that will be used if the caller does not supply an argument for that parameter. This feature makes your code more flexible and concise, as you do not have to check for undefined or supply values manually. Default parameters are especially useful when a function argument is often the same value, but you want to allow callers to override it if needed.

123456
function multiply(a: number, b: number = 1): number { return a * b; } console.log(multiply(5)); // Output: 5 console.log(multiply(5, 3)); // Output: 15
copy

When you assign a default value to a parameter, TypeScript automatically infers the parameter's type from the default value if you do not specify it explicitly. However, it is best practice to always annotate the parameter type for clarity and to prevent unexpected behaviors. In the example above, b is both typed as number and given a default value of 1, so TypeScript ensures that only numbers are accepted. If you omit the type annotation, TypeScript will use the type of the default value, but being explicit makes your code easier to read and maintain. Default parameters can be used with other parameter types and combined with optional parameters, but always place them after required parameters to avoid confusion.

question mark

What is the main purpose of default parameters in TypeScript functions?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain the difference between default and optional parameters in TypeScript?

Are there any best practices for using default parameters in functions?

Can default parameters be used with other parameter types, like objects or arrays?

Awesome!

Completion rate improved to 9.09

bookDefault Parameters

Scorri per mostrare il menu

Default parameters let you provide a value for a function parameter that will be used if the caller does not supply an argument for that parameter. This feature makes your code more flexible and concise, as you do not have to check for undefined or supply values manually. Default parameters are especially useful when a function argument is often the same value, but you want to allow callers to override it if needed.

123456
function multiply(a: number, b: number = 1): number { return a * b; } console.log(multiply(5)); // Output: 5 console.log(multiply(5, 3)); // Output: 15
copy

When you assign a default value to a parameter, TypeScript automatically infers the parameter's type from the default value if you do not specify it explicitly. However, it is best practice to always annotate the parameter type for clarity and to prevent unexpected behaviors. In the example above, b is both typed as number and given a default value of 1, so TypeScript ensures that only numbers are accepted. If you omit the type annotation, TypeScript will use the type of the default value, but being explicit makes your code easier to read and maintain. Default parameters can be used with other parameter types and combined with optional parameters, but always place them after required parameters to avoid confusion.

question mark

What is the main purpose of default parameters in TypeScript functions?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2
some-alt