TypeScript — Functions

TypeScript — Functions

In this lesson, we are going to learn about functions, function type in TypeScript, spread operator, rest parameters, function overloading, and other function related lessons.

In the above example, we have declared a function with the name sum that accepts argument a and b. It performs arithmetic sum operation on these two operands and returns a result.

In an ideal situation, these two arguments must be numbers or values of the type number to be precise. But JavaScript doesn’t stop you from passing any other values as arguments to the function call. That’s why sometimes you can see [NaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN) as the return value when arithmetic operations are performed on non-number values.

Also, we expect the return value of this function to be a number but JavaScript won’t stop you from treating it differently. For example, in the above program, we have called [split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) method on the expected return value of type number but is a prototype method of a string value. Luckily, the return value was a string (not ideal though) and our program did not crash.

As you can see, the program above is not ideal to run in mission-critical situations. We need to know if our program can work safely before it is deployed to the mission. This is where TypeScript can help us. Before compiling the program, TypeScript walks through each function call of sum and checks if arguments are all right. Let’s redesign the program above.

web-development typescript programming javascript nodejs

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Nodejs Developer

Looking to **hire NodeJS Developer**[https://www.dataeximit.com/node-js-development/](https://www.dataeximit.com/node-js-development/ "https://www.dataeximit.com/node-js-development/") to build a perfect back-end to your website or...

Typescript — Hello World Program

In this lesson, we are going to learn about the basic structure of a TypeScript program and understand a few concepts of the compilation process. Then we will see how we can run the compiled JavaScript program using node and ts-node.

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Web Development 2020 vs 2010

Things have changed pretty fast from what was a decade ago for web development and the facts are totally mind-blowing. I mean a web developer now makes mobile apps, desktop app, backend API, games and so much more.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.