Pass optional function parameter in Typescript

You can use undefined for the non defined parameter:


function addNumbers(a: string, b: string, message: string) {
   return `${message} ${a + b}`;
}

const result = addNumbers(10, 17, undefined)
// result = 27

Because I set the message parameter to undefined the result will be in this case27.
You can also use the question mark close to the parametermessage?:string without using the undefined value when you use the function.


function addNumbers(a: string, b: string, message?: string) {
   return `${message} ${a + b}`;
}

const result = addNumbers(10, 17)
// result = 27