1.

Provide the syntax for optional parameters in TypeScript.

Answer»

A FUNCTION can mark ONE or more of its PARAMETERS as optional by suffixing its name with ‘?’. In the example below, the PARAMETER greeting is marked optional.

function greet(name: string, greeting?: string) {if (!greeting) greeting = "Hello";console.log(`${greeting}, ${name}`);}greet("John", "HI"); // Hi, Johngreet("Mary", "Hola"); // Hola, Marygreet("Jane"); // Hello, Jane


Discussion

No Comment Found