Skip to content

Functions and Classes#

Functions#

Function Declaration#

function sum(x: number, y: number): number {
    return x + y;
};

console.log(`Declaration: ${sum(10, 10)}`);

Function Expression#

let sum2 = function (x: number, y: number): number {
    return x + y;
};

console.log(`Expression: ${sum(20, 20)}`);

Result: result

Arrow Function#

Used for anonymous functions

(param1,..., paramN) => expression

Used =>, passed parameters in (), and expression enclosed within {}

let test = (x: string): string => {
    return x;
};

console.log(`Arrow function: ${test("HI")}`);

without parameters:

let print = () => console.log("Without parameters: HI");

print();

Result: result