Show List
TypeScript Functions
TypeScript allows to add type to the function parameters and return value.
For example below is a JavaScript function in which we do not specify the data type of function parameters or the value returned by the function.
function addNumbers(a, b) {return a + b;}console.log(addNumbers(2, 5));
This same function can be written in the TypeScript as below. Here we specify the data type so if there is code to call this function with values of incorrect data type, compiler will throw the error.
myFunction.ts
function addNumbers(a: number, b: number):number {return a + b;}console.log(addNumbers(2,5))
To run this code, compile it using TypeScript compiler and then run the generated JavaScript file using Node
PS C:\Users\mail2\Downloads\demo> tsc myFunction.ts PS C:\Users\mail2\Downloads\demo> node myFunction.js 7
Optional Parameters
Optional parameters can be marked with ?
Example:
function addNumbers(a: number, b: number, c?: number): number {return a + b + (c || 0);}console.log(addNumbers(2, 5))
Output
PS C:\Users\mail2\Downloads\demo> tsc myFunction.ts PS C:\Users\mail2\Downloads\demo> node myFunction.js 7
Default Value for Parameter
Default value can be assigned to the parameters after the type annotation
function offer_price(price:number, discount:number = 0.10) {var offer_price = price * (1 - discount);console.log("Today's offer price : ",offer_price);}offer_price(100)offer_price(100, 0.2)
Output
PS C:\Users\mail2\Downloads\demo> tsc myFunction.ts PS C:\Users\mail2\Downloads\demo> node myFunction.js Today's offer price : 90 Today's offer price : 80
Variable Arguments /Rest Parameters
Variable arguments parameter is always an array
function addNumbers(a: number, b: number, ...nums: number[]) {var sum: number = a + b;for (let i = 0; i < nums.length; i++) {sum = sum + nums[i];}console.log("sum = ", sum)}addNumbers(2, 5)addNumbers(1, 3, 6, 9)
Output
PS C:\Users\mail2\Downloads\demo> tsc myFunction.ts PS C:\Users\mail2\Downloads\demo> node myFunction.js sum = 7 sum = 19
Anonymous Function
Anonymous functions do not have a name and are stored in a variable. They are always called using the variable name.
const addNumbers = function (a: number, b: number, ...nums: number[]) {var sum: number = a + b;for (let i = 0; i < nums.length; i++) {sum = sum + nums[i];}console.log("sum = ", sum)}addNumbers(2, 5)addNumbers(1, 3, 6, 9)
Output
PS C:\Users\mail2\Downloads\demo> tsc myFunction.ts PS C:\Users\mail2\Downloads\demo> node myFunction.js sum = 7 sum = 19
Arrow Function
Arrow function is a shorter syntax to write function. Function keyword or return keyword are not required in this.
const addNumbers = (a: number, b: number, ...nums: number[]) => {var sum: number = a + b;for (let i = 0; i < nums.length; i++) {sum = sum + nums[i];}console.log("sum = ", sum)}addNumbers(2, 5)addNumbers(1, 3, 6, 9)
Output
PS C:\Users\mail2\Downloads\demo> tsc myFunction.ts PS C:\Users\mail2\Downloads\demo> node myFunction.js sum = 7 sum = 19
Leave a Comment