typescript function with default parmeters code example

Example 1: how to make a parameter optional in typescript

// Optional parameter
function foo(x?: number) {
    console.log("x : "+ x);
}
foo();
foo(6);

Example 2: simple function in typescript

// Named function

//function with type as number
function add(x: number, y: number): number {
  // return sum of numbers entered as params
  return x + y;
}

// Anonymous function

// variable to call and define function
let myAdd = function (x: number, y: number): number {
  // return sum of numbers entered as params
  return x + y;
};