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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 2

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Awesome!

Completion rate improved to 9.09

bookDefault Parameters

Veeg om het menu te tonen

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 2
some-alt