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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 9.09

bookOptional Parameters

Scorri per mostrare il menu

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1
some-alt