• Home
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • TypeScript
      • Introduction to TypeScript
      • TypeScript Developer
    • React
      • Introduction to React
      • React Developer
    • Python
      • Introduction to Python
      • Python Developer
    • C++
      • Introduction to C++
      • C++ Developer
    • C Language
      • Introduction to C
      • C Developer
    • Rust
      • Introduction to Rust
      • Rust Developer
    • Zig
      • Introduction to Zig
      • Zig Developer
    • Linux Shell
      • Introduction to the Linux Shell
      • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Reading Grounds

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds
  • Featured on DanielLaunches

Courses

  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

Lesson 1

Lesson not found

Default Parameters

Lesson Progress: 0%

Lesson Progress: 0%
Default Parameters
Lesson Incomplete
Lesson not found

Default parameters let you specify a fallback value for a function parameter when the caller does not provide one. You write the default value right after the parameter firstName using an equals sign, like name: string = "Guest". If the caller passes an argument, the default is ignored and the passed value is used. If the caller skips that argument, the default value is used instead. In TypeScript, the default value must match the parameter's type annotation, so the compiler can still check for type safety. Default parameters make your functions more flexible because they work both with and without arguments. They also help you avoid errors from missing arguments by ensuring a sensible value is always available.

Code Example
function greet(firstName: string = "Guest"): string {
  return "Hello " + firstName;
}
console.log(greet("Alice"));
console.log(greet());

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Typescript Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here

A default parameter provides a fallback string when no argument is passed.

  • Naming a variable name, should often be avoided in browser enviroments, since it can interfere with the built-in global variable window.name
  • The parameter firstName has a default value of "Guest".
  • When greet("Alice") is called, the argument "Alice" overrides the default.
  • When greet() is called with no argument, firstName gets the default "Guest".
  • The return type is : string, and the default "Guest" is a string, so the type is always satisfied.
  • Default parameters let you call the same function with or without arguments.
Code Example
function multiply(a: number, b: number = 2): number {
  return a * b;
}
console.log(multiply(5));
console.log(multiply(5, 3));

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Typescript Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here

A default parameter can be a number, and it works alongside required parameters.

  • The parameter b has a default of 2, while a has no default.
  • Calling multiply(5) uses the default 2 for b, giving 5 * 2 = 10.
  • Calling multiply(5, 3) overrides the default with 3, giving 5 * 3 = 15.
  • Parameters with defaults can come after required parameters in the parameter list.
  • TypeScript checks that the default value matches the parameter's type annotation.
Code Example
function createUser(firstName: string, isAdmin: boolean = false): string {
  if (isAdmin) {
    return firstName + " (admin)";
  }
  return firstName + " (user)";
}
console.log(createUser("Alice"));
console.log(createUser("Bob", true));

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Typescript Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here

Default parameters work with boolean values too, letting you set a default state.

  • The parameter isAdmin defaults to false, so most users are not admins by default.
  • Calling createUser("Alice") skips the second argument, so isAdmin is false.
  • Calling createUser("Bob", true) passes true, making Bob an admin.
  • Inside the function, an if statement checks isAdmin to choose the right message.
  • Default booleans are useful for optional features or permission settings.
Code Example
function logMessage(msg: string, prefix: string = "Info"): string {
  return prefix + ": " + msg;
}
console.log(logMessage("Started"));
console.log(logMessage("Failed", "Error"));

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Typescript Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here

Default parameters can be used to set a prefix or label, making output customizable.

  • The parameter prefix defaults to "Info", so messages get a standard label.
  • Calling logMessage("Started") uses the default prefix and returns "Info: Started".
  • Calling logMessage("Failed", "Error") overrides the prefix with "Error".
  • The first parameter msg is required, so you must always provide a message.
  • This pattern is common for logging, where some details are optional but the main message is required.
Code Example
function calculateTotal(price: number, tax: number = 0.1): number {
  return price + price * tax;
}
console.log(calculateTotal(100));
console.log(calculateTotal(100, 0.2));

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Typescript Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here

Default parameters can hold values used in calculations, like a default tax rate.

  • The parameter tax defaults to 0.1, representing a 10 percent tax.
  • Calling calculateTotal(100) uses the default tax, giving 100 + 100 * 0.1 = 110.
  • Calling calculateTotal(100, 0.2) overrides the tax rate to 20 percent, giving 120.
  • The required parameter price must always be provided.
  • Default values for calculations let you set sensible defaults while allowing customization.
Code Example
const greet6 = (firstName: string = "Guest"): string => "Hi " + firstName;
console.log(greet6("Alice"));
console.log(greet6());

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Typescript Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here

Arrow functions can also have default parameters, keeping the concise syntax while adding flexibility.

  • The arrow function uses = to set a default value for firstName right in the parameter list.
  • When called with "Alice", the default is overridden and the output is "Hi Alice".
  • When called with no argument, the default "Guest" is used, giving "Hi Guest".
  • Arrow functions with defaults work the same way as regular function declarations.
  • This combination gives you both the short arrow syntax and the safety of a fallback value.

Test Incomplete

What does a default parameter do in TypeScript?

Question #

1/15

Score

0/0 - 0.0 %