codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

TypeScript

12 / 60 topics
8Functions in TypeScript9Function Parameters10Return Types11Optional Parameters12Default Parameters13Rest Parameters14Arrow Functions
Tutorials/TypeScript/Default Parameters
🔷TypeScript

Default Parameters

Updated 2026-05-15
10 min read

Default Parameters

Introduction

In TypeScript, just like in JavaScript, you can set default values for function parameters. This feature allows functions to be called with fewer arguments than they are defined to accept. If a parameter is not provided when the function is invoked, its default value will be used instead.

Default parameters are particularly useful for making functions more flexible and reducing the need for conditional checks within the function body.

Concept

When you define a function in TypeScript (or JavaScript), you can specify default values for some or all of its parameters. These defaults come into play when the function is called without providing arguments for those specific parameters.

Here's the basic syntax for defining default parameters:

function functionName(param1 = defaultValue1, param2 = defaultValue2) {
  // Function body
}

In this example, `param1` will use `defaultValue1` if no argument is passed for it when the function is called. Similarly, `param2` will use `defaultValue2`.

## Examples

Let's explore some practical examples to understand how default parameters work in TypeScript.

### Example 1: Basic Usage

```typescript
function greet(name = "Guest") {
  return `Hello, \${name}!`;
}

console.log(greet()); // Output: Hello, Guest!
console.log(greet("Alice")); // Output: Hello, Alice!

In this example, the greet function has a default parameter name set to "Guest". When greet() is called without any arguments, it uses the default value. However, when greet("Alice") is called, it overrides the default with the provided argument.

Example 2: Multiple Default Parameters

function createPerson(firstName = "John", lastName = "Doe") {
  return { firstName, lastName };
}

console.log(createPerson()); // Output: { firstName: 'John', lastName: 'Doe' }
console.log(createPerson("Alice")); // Output: { firstName: 'Alice', lastName: 'Doe' }
console.log(createPerson("Bob", "Smith")); // Output: { firstName: 'Bob', lastName: 'Smith' }

Here, both `firstName` and `lastName` have default values. You can provide one or both arguments when calling the function.

### Example 3: Default Parameters with Expressions

Default parameters can also be expressions:

```typescript
function calculateDiscount(price, discount = price * 0.1) {
  return price - discount;
}

console.log(calculateDiscount(100)); // Output: 90 (default 10% discount)
console.log(calculateDiscount(100, 20)); // Output: 80 (custom 20% discount)

In this example, the discount parameter defaults to 10% of the price. However, you can override it by providing a custom discount value.

Example 4: Default Parameters with Function Calls

You can even use function calls as default values:

function getGreeting() {
  return "Hello";
}

function greetWithCustomMessage(message = getGreeting()) {
  console.log(`\${message}, world!`);
}

greetWithCustomMessage(); // Output: Hello, world!
greetWithCustomMessage("Hi"); // Output: Hi, world!

Here, the getGreeting function is called to set the default value for message.

What's Next?

Now that you've learned about default parameters, you might want to explore another powerful feature of TypeScript and JavaScript: Rest Parameters. Rest parameters allow a function to accept any number of arguments as an array, providing even more flexibility in handling input.

Stay tuned for the next section where we dive into rest parameters!


PreviousOptional ParametersNext Rest Parameters

Recommended Gear

Optional ParametersRest Parameters