RSRobince Studio

Optional and Default

A question mark after a name marks a property or parameter as optional, so it may be left out.

A default parameter supplies a fallback value when an argument is not passed to a function.

interface User {
  name: string;
  age?: number;
}
function greet(who = "friend") {
  return "Hi " + who;
}
An optional property may be undefined, so check it before use.
Try it yourself

Loading editor…