Skip to content
Justice Tech School

Optional and Default Parameters

Mark a parameter optional with ? or give a default value. Optional ones must come last.

Example
function hi(name: string, greeting = "Hello", end?: string) {
  return greeting + " " + name + (end ?? "");
}
console.log(hi("Ada"), hi("Ada", "Hi", "!"));

Result

Hello Ada Hi Ada!

Exercise

Optional param.

function f(a: number) {}

Quiz Test

Where do optional params go?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.