Default Parameters In TypeScript

[!INFO]-
topic: πŸ¦’ TypeScript
links: Optional Parameters in TypeScript
source: TypeScript course by the native web
tags: #permanent-noteΒ #published

Last Modified: =dateformat(this.file.mtime, "MMM. dd, yyyy - HH:mm")


You can assign a default value to a function so that it is assigned if you don't explicitly assign anything.

// Parameters with default values
const getHost = function (hostname: string, port: number = 443): string {
  return `${hostname}:${port}`;
};

const localhost = getHost('localhost', 3000);
const yourDomain = getHost('yourdomain.com');

console.log(yourDomain);