Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen 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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 9.09

bookDefault Parameters

Swipe um das Menü anzuzeigen

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
some-alt