Lesson 23 of 60 – Optional Parameters
38%

Optional Parameters in TypeScript

An optional parameter is a function parameter that does not have to be provided when the function is called. In TypeScript, we use the ? symbol after the parameter name to make it optional.

Note: Optional parameters are useful when a function can work with or without a particular value.

1. What is an Optional Parameter?

An optional parameter is a parameter that can be omitted when calling a function.

function greet(name?: string) {
    console.log(name);
}

The ? makes name optional.

2. Optional Parameter Syntax

Place the question mark after the parameter name and before the colon.

function show(name?: string) {
    console.log(name);
}

The general syntax is:

parameterName?: type

3. Calling a Function Without an Optional Parameter

Because the parameter is optional, the function can be called without providing it.

function greet(name?: string) {
    console.log(name);
}

greet();

The function call is valid.

4. Calling with an Optional Parameter

You can also provide a value for the optional parameter.

function greet(name?: string) {
    console.log(name);
}

greet("Amit");

The function works whether the parameter is provided or omitted.

5. Optional Parameter May Be undefined

When an optional parameter is not provided, its value can be undefined.

function greet(name?: string) {
    console.log(name);
}

greet();

Inside the function, name may be a string or undefined.

6. Optional Parameter with a Return Type

Optional parameters can be combined with a function return type.

function greet(name?: string): string {
    return "Hello " + name;
}

The function returns a string.

7. Checking an Optional Parameter

You can check whether an optional parameter has a value.

function greet(name?: string): void {

    if (name) {
        console.log("Hello " + name);
    }

}

8. Using undefined Check

You can explicitly check whether an optional parameter is undefined.

function showName(name?: string): void {

    if (name !== undefined) {
        console.log(name);
    }

}

9. Optional Number Parameter

Optional parameters can also be numbers.

function showAge(age?: number): void {
    console.log(age);
}

showAge();
showAge(20);

10. Optional Boolean Parameter

A boolean parameter can also be optional.

function showStatus(
    active?: boolean
): void {
    console.log(active);
}

showStatus();
showStatus(true);

11. Multiple Optional Parameters

A function can have multiple optional parameters.

function student(
    name?: string,
    age?: number
): void {
    console.log(name, age);
}

Both parameters can be omitted.

12. Calling with Multiple Optional Parameters

You can provide one or both optional parameters.

function student(
    name?: string,
    age?: number
): void {
    console.log(name, age);
}

student();
student("Amit");
student("Amit", 20);

13. Required Parameter Before Optional Parameter

A required parameter can appear before an optional parameter.

function greet(
    name: string,
    age?: number
): void {
    console.log(name, age);
}

The name parameter is required, while age is optional.

14. Calling Required and Optional Parameters

When a required parameter comes first, it must be provided.

function greet(
    name: string,
    age?: number
): void {
    console.log(name, age);
}

greet("Rahul");
greet("Rahul", 21);

15. Optional Parameter and Default Value

A default parameter automatically provides a value when an argument is omitted.

function greet(
    name: string = "Guest"
): void {
    console.log("Hello " + name);
}

Default parameters and optional parameters solve related problems in different ways.

16. Optional Parameter vs Default Parameter

An optional parameter may be undefined when omitted. A default parameter receives a specified default value.

function optional(name?: string) {
    console.log(name);
}

function defaultName(name: string = "Guest") {
    console.log(name);
}

17. Optional Parameter with String Operations

Because an optional string may be undefined, check it before using string methods.

function upperName(name?: string): void {

    if (name !== undefined) {
        console.log(name.toUpperCase());
    }

}

18. Optional Parameter with Number Calculation

Check an optional number before performing calculations.

function double(value?: number): number {

    if (value !== undefined) {
        return value * 2;
    }

    return 0;
}

19. Optional Parameter with Object

An object can also be an optional parameter.

function showStudent(
    student?: {
        name: string;
        age: number;
    }
): void {

    if (student) {
        console.log(student.name);
    }

}

20. Optional Parameter with Array

An array parameter can also be optional.

function showNumbers(
    numbers?: number[]
): void {

    if (numbers) {
        console.log(numbers);
    }

}

21. Optional Parameters in Arrow Functions

Arrow functions can also have optional parameters.

const greet = (
    name?: string
): void => {
    console.log(name);
};

greet();
greet("Amit");

22. Optional Parameters in React

Optional parameters can be useful in React utility functions where some information may not always be available.

function showMessage(
    message?: string
): void {

    if (message) {
        console.log(message);
    }

}

23. Optional Parameters in React Native

React Native applications often use functions that may receive optional information.

function openScreen(
    screenName: string,
    userId?: number
): void {

    console.log(screenName, userId);

}

24. Optional User ID

An optional user ID can be useful when a function supports both general and user-specific actions.

function loadProfile(
    userId?: number
): void {

    if (userId !== undefined) {
        console.log("Loading user:", userId);
    } else {
        console.log("No user selected");
    }

}

25. Optional Parameter with Interface

An interface can be used as the type of an optional parameter.

interface User {
    name: string;
    age: number;
}

function showUser(
    user?: User
): void {

    if (user) {
        console.log(user.name);
    }

}

26. Common Mistake

A common mistake is using an optional parameter directly without considering that it may be undefined.

function greet(name?: string) {

    // Check first
    if (name !== undefined) {
        console.log(name.toUpperCase());
    }

}

Always handle the possibility of an undefined value.

27. Optional Parameter and Null

An optional parameter is not automatically the same as explicitly allowing null.

function show(
    value?: string
): void {
    console.log(value);
}

If you also want to explicitly allow null, include it in the type.

function show(
    value?: string | null
): void {
    console.log(value);
}

28. When to Use Optional Parameters

Optional parameters are useful when:

  • A value is not always required.
  • A function supports different calling situations.
  • A value may not be available.
  • You want to make a function flexible.
function sendMessage(
    message: string,
    recipient?: string
): void {
    console.log(message, recipient);
}

29. Complete Practical Example

Here is a practical example using a required parameter and an optional parameter.

function createStudent(
    name: string,
    age?: number
): string {

    if (age !== undefined) {
        return name + " is " + age + " years old.";
    }

    return name + " age is not provided.";
}

console.log(createStudent("Amit"));
console.log(createStudent("Rahul", 21));

30. Optional Parameters Summary

Optional parameters allow a function parameter to be omitted when calling the function.

function greet(
    name?: string
): void {

    if (name !== undefined) {
        console.log("Hello " + name);
    } else {
        console.log("Hello Guest");
    }

}

Use the ? symbol after the parameter name when the parameter is optional.

📌 Key Points

  • Use ? to make a function parameter optional.
  • An optional parameter can be omitted when calling a function.
  • An omitted optional parameter can have the value undefined.
  • Required parameters can come before optional parameters.
  • Optional parameters can have types such as string, number, boolean, array, or object.
  • Always consider the possibility of undefined when using optional parameters.
  • Optional parameters and default parameters are related but work differently.
  • Optional parameters can be used in normal functions and arrow functions.
  • They are useful in React and React Native applications.

🧠 Quick Quiz

Question: Which symbol is used to make a function parameter optional in TypeScript?