Default 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.
123456function multiply(a: number, b: number = 1): number { return a * b; } console.log(multiply(5)); // Output: 5 console.log(multiply(5, 3)); // Output: 15
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.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
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
Default Parameters
Svep för att visa menyn
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.
123456function multiply(a: number, b: number = 1): number { return a * b; } console.log(multiply(5)); // Output: 5 console.log(multiply(5, 3)); // Output: 15
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.
Tack för dina kommentarer!