WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

TypeScript · Intermediate · question 22 of 100

How does TypeScript support default parameters in functions?

📕 Buy this interview preparation book: 100 TypeScript questions & answers — PDF + EPUB for $5

TypeScript, like modern JavaScript versions (ES6/ECMAScript 2015), supports default parameters in functions. Default parameter values are assigned to a function’s formal parameters when the function is called without providing specific arguments for those parameters. This can help you write more flexible and concise functions, as well as make your code more readable.

Here’s a brief syntax overview of assigning default values to parameters in TypeScript:

function functionName(parameterName: type = defaultValue): returnType {
  // function body
}

You can assign default values to any number of parameters within a function. Let’s look at an example function with default parameter values:

function greet(name: string, greeting: string = 'Hello'): string {
  return `${greeting}, ${name}!`;
}

In this example, the function ‘greet‘ takes two parameters: ‘name‘ and ‘greeting‘. If the ‘greeting‘ parameter is not provided when calling the function, it will default to the string ‘’Hello’‘.

Let’s see how this works with different function calls:

console.log(greet('Alice')); // "Hello, Alice!"
console.log(greet('Bob', 'Hi')); // "Hi, Bob!"

When calling ‘greet(’Alice’)‘, no ‘greeting‘ argument is provided, so the default ‘greeting‘ value ‘’Hello’‘ is used in the function body. The output of ‘greet(’Bob’, ’Hi’)‘ is ‘’Hi, Bob!’‘, as the ‘greeting‘ argument ‘’Hi’‘ is used instead of the default value.

Here’s a summary of how default parameters work in TypeScript:

Default parameters in TypeScript:

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic TypeScript interview — then scores it.
📞 Practice TypeScript — free 15 min
📕 Buy this interview preparation book: 100 TypeScript questions & answers — PDF + EPUB for $5

All 100 TypeScript questions · All topics