Provide the syntax for optional parameters in TypeScript Last Updated : 22 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In TypeScript, optional parameters allow you to specify that a function parameter may be omitted when calling the function. You denote optional parameters by adding a question mark (?) after the parameter name in the function declaration. Syntax:function functionName(param1: type, param2?: type, param3?: type) { // Function body } Parameters:param1 is a required parameter.param2 and param3 are optional parameters.When calling the function, you can provide values only for the required parameters or include values for optional parameters as needed. Using optional parameters in the functionExample 1: Here is a simple example of a function to add two numbers. The num2 is considered optional. In the program, we make sure that num2 has some value passed in by checking if it's not undefined. So there are two cases, one is if we pass a value to the optional parameter or if only the required values have values associated with them. JavaScript // Function to add two numbers function add(num1: number, num2?: number): number { if (typeof num2 !== "undefined") { return num1 + num2; } return num1; } // Function call console.log(add(2, 3)); console.log(add(9)); Output: 5 9 Optional parameters must appear after the required parametersExample 2:In the below example we contradict the statement by specifying optional parameters before the main ones, typescript compiler raised an error saying "A required parameter cannot follow an optional parameter". JavaScript // Add function function add(num1?: number, num2: number): number { if (typeof num2 !== "undefined") { return num1 + num2; } return num1; } // Function call console.log(add(2, 3)); console.log(add(9)); Output: error TS1016: A required parameter cannot follow an optional parameter. function add(num1?: number, num2: number): number { .. } SummaryOptional Parameters Syntax:In TypeScript, use the parameter?: type syntax to declare optional parameters in a function.Example: function exampleFunction(param1: string, param2?: number) { /* function body */ }Checking Initialization:To check if an optional parameter has been initialized within the function, use the expression typeof(parameter) !== 'undefined'.This check ensures that the optional parameter has a value before using it in the function logic.Reference: https://www.typescriptlang.org/docs/handbook/functions.html#optional-and-default-parameters Comment More infoAdvertise with us Next Article TypeScript Optional Parameters in Callbacks I isitapol2002 Follow Improve Article Tags : TypeScript Geeks-Premier-League-2022 JavaScript-Questions Similar Reads How to Specify Optional Properties in TypeScript? TypeScript is a powerful programming language that extends JavaScript by adding optional static typing and class-based object-oriented programming. One of the key features of TypeScript is the ability to specify optional properties in interfaces and classes, providing flexibility to our object types 3 min read TypeScript Optional Parameters Optional parameters in TypeScript allow functions to be called without specifying all arguments, enhancing flexibility and code readability.Denoted by appending a ? to the parameter name.Optional parameters must follow the required parameters in function definitions.Syntaxfunction functionName(param 3 min read TypeScript Optional Parameters in Callbacks In TypeScript, optional parameters in callbacks are parameters that may or may not be provided when the callback function is invoked. They are denoted by a ? after the parameter name, allowing for more flexible function calls without requiring all arguments.Syntaxtype MyCallback = (param1: string, p 2 min read TypeScript Optional Properties Type TypeScript Opional properties type provides a way of defining the parts that are not necessarily required.TypeScript Optional Properties TypesOptional Properties are properties that are not required mandatorily and can be omitted when not needed.In TypeScript, you can define optional properties in a 6 min read TypeScript Object Type Optional Properties In TypeScript, optional properties are denoted using the ? modifier after the property name in the object type definition. This means that when an object is created, the optional property can either be provided or omitted.Syntax:type TypeName = { propertyName: PropertyType; optionalPropertyName?: Op 3 min read TypeScript Parameter Type Annotations TypeScript Parameter type annotations are used to specify the expected data types of function parameters. They provide a way to explicitly define the types of values that a function expects as arguments. Parameter type annotations are part of TypeScript's static typing system, and they help catch ty 2 min read Like