Lambda functions in TypeScript

lambda functions help remove ‘function’ and define functions inline – check the blue text on TypeScript website, which will give the output in red…

let a: number, b: number;
a = 23;
b = 14;
/* No parameters… simple return */
var simpleTen = () => { return 10; };
console.log(“Now calling simpleTen ” + simpleTen() );

var a, b;
a = 23;
b = 14;
/* No parameters… simple return */
var simpleTen = function () { return 10; };
console.log(“Now calling simpleTen ” + simpleTen());

/* One parameters… simple return */
let squareNumber: any = (a: number) => { squareNumber = a * a; return squareNumber;};
console.log(“Now calling squareNumber(“+a+”) is ” + squareNumber(a) );

/* One parameters… simple return */
var squareNumber = function (a) { squareNumber = a * a; return squareNumber; };
console.log(“Now calling squareNumber(” + a + “) is ” + squareNumber(a));

/* Two parameters… simple return */
let additionFunc = (n1: number, n2: number): number => {
/*let varTest = 10 + n1 + n2;
console.log(“(inside) additionFunc (with additional 10) is… ” + varTest); */
return (n1 + n2);
};
console.log(“Now calling additionFunc(“+a+”, “+b+”) is ” + additionFunc(a,b) );

/* Two parameters… simple return */
var additionFunc = function (n1, n2) {
/*let varTest = 10 + n1 + n2;
console.log(“(inside) additionFunc (with additional 10) is… ” + varTest); */
return (n1 + n2);
};
console.log(“Now calling additionFunc(” + a + “, ” + b + “) is ” + additionFunc(a, b));

var foo = () => ({ “name”: “test Name”, “age”: 21 });
console.log(“Now calling foo (which is a variable that contains the functiona and will hence return the function): ” + foo);
console.log(“Now calling foo() (which is a function call and will CALL the function to return an object)… [Name]:” + foo().name + ” & [age]: ” + foo().age);

var foo = function () { return ({ “name”: “test Name”, “age”: 21 }); };
console.log(“Now calling foo (which is a variable that contains the functiona and will hence return the function): ” + foo);
console.log(“Now calling foo() (which is a function call and will CALL the function to return an object)… [Name]:” + foo().name + ” & [age]: ” + foo().age);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s