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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 9.09

bookDefault Parameters

Deslize para mostrar o 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
some-alt