HomeToolsAbout a20k

Functions

Function

Param Syntax

export function twoFer(param: int): string { ... }
  • (): string is called a return type

    • It's a type of the value that is returned from a function
  • (param: int) represents the parameters for the function

    • Parameters need their own typing
  • export allows the test to import the function and call it

// Optional Parameter `y` function newFunc(x: number, y?: number) { ... } // Default Parameter function newFunc(name: string = "you"){ ... } function applyDiscount(price: number, discount: number = 0.05): number { return price * (1 - discount); } console.log(applyDiscount(100)); // 95

Return Types

// void return type function messageLogger(message: string): void { console.log(message); } // primitive return type function addNumbers(a: number, b: number): number { return a + b; }

Function Expression

Function Expression with number return type

const addNumbers = (a: number, b: number): number => a + b;
© VincentVanKoh