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

bookOptional Parameters

When working with functions in TypeScript, you may encounter situations where not all parameters are required for every call. To handle such cases, TypeScript allows you to define optional parameters. An optional parameter is a parameter that does not need to be provided when calling a function. You define an optional parameter by adding a question mark (?) after the parameter name in the function signature. This tells TypeScript that the parameter may be omitted or explicitly passed as undefined.

12345678910
function printName(first: string, last?: string): void { if (last) { console.log("Full name:", first + " " + last); } else { console.log("First name:", first); } } printName("Alice", "Smith"); // Output: Full name: Alice Smith printName("Bob"); // Output: First name: Bob
copy

In the printName function above, the last parameter is marked as optional. This means you can call printName with just the first argument, or with both first and last. TypeScript will enforce that any optional parameter must come after all required parameters in the function signature. When you omit an optional parameter, its value inside the function is undefined. This requires you to handle the possibility that the parameter may not be present, as shown by the conditional check in the function body.

Optional parameters provide flexibility in how functions can be called, but they also require you to consider what happens when a value is missing. TypeScript's static checking ensures you do not accidentally skip required parameters, and helps you catch errors related to parameter order and usage. By marking parameters as optional, you can design functions that are both robust and easy to use in a variety of scenarios.

question mark

Which of the following correctly defines an optional parameter in a TypeScript function?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

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

Suggested prompts:

Can you explain what happens if I pass `undefined` explicitly as the optional parameter?

Are there any best practices for using optional parameters in TypeScript?

Can I have multiple optional parameters in a function?

Awesome!

Completion rate improved to 9.09

bookOptional Parameters

Swipe um das Menü anzuzeigen

When working with functions in TypeScript, you may encounter situations where not all parameters are required for every call. To handle such cases, TypeScript allows you to define optional parameters. An optional parameter is a parameter that does not need to be provided when calling a function. You define an optional parameter by adding a question mark (?) after the parameter name in the function signature. This tells TypeScript that the parameter may be omitted or explicitly passed as undefined.

12345678910
function printName(first: string, last?: string): void { if (last) { console.log("Full name:", first + " " + last); } else { console.log("First name:", first); } } printName("Alice", "Smith"); // Output: Full name: Alice Smith printName("Bob"); // Output: First name: Bob
copy

In the printName function above, the last parameter is marked as optional. This means you can call printName with just the first argument, or with both first and last. TypeScript will enforce that any optional parameter must come after all required parameters in the function signature. When you omit an optional parameter, its value inside the function is undefined. This requires you to handle the possibility that the parameter may not be present, as shown by the conditional check in the function body.

Optional parameters provide flexibility in how functions can be called, but they also require you to consider what happens when a value is missing. TypeScript's static checking ensures you do not accidentally skip required parameters, and helps you catch errors related to parameter order and usage. By marking parameters as optional, you can design functions that are both robust and easy to use in a variety of scenarios.

question mark

Which of the following correctly defines an optional parameter in a TypeScript function?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1
some-alt