Typescript
TypeScript Function, Return types, Rest Parameters
- Function Declaration and Parameters
- Return Types
- Optional and Default Parameters
- Rest Parameters
- Conclusion
Functions are an essential part of any programming language, including TypeScript. They allow you to group together a set of statements that perform a specific task, and can be called multiple times throughout your code.
In this article, we will explore the different aspects of functions in TypeScript, including function declaration and parameters, return types, optional and default parameters, and rest parameters.
Note: The function declaration, parameters, return types, optional, default parameters are almost same as JavaScript, just added types with it in TypeScript.
¶Function Declaration and Parameters
In TypeScript, you can declare a function using the function keyword same as JavaScript, followed by the name of the function and a set of parentheses that contain any parameters for the function.
Here’s an example:
function add(a: number, b: number) {
return a + b;
}
In this example, we declare a function called add()
that takes two parameters, both of type number
. Inside the function, we add the two parameters together and return the result a + b
.
You can also declare the type of the function parameters using type annotations, as shown in the example above. Variable a
type is number
and variable b
type is also number
.
a: number, b: number
¶Return Types
In TypeScript, you can also specify the return type of a function using a type annotation. Here’s an example:
function multiply(a: number, b: number): number {
return a * b;
}
In this example, we define a function called multiply()
that takes two parameters of type number
, and returns a value of type number
. Inside the function, we multiply the two parameters together and return the result a * b
.
¶Optional and Default Parameters
In TypeScript, you can make a function parameter optional by adding a question mark ?
after its name
. Here’s an example:
function greet(name?: string) {
if (name) {
console.log(`Hello, ${name}!`);
} else {
console.log(`Hello, stranger!`);
}
}
In this example, we define a function called greet()
that takes an optional parameter called name
of type string. If the parameter is provided, we log a greeting message that includes the name. If the parameter is not provided, we log a generic greeting message.
You can also provide default values for function parameters by assigning a value to them in the parameter list. Here’s an example:
function calculateDiscount(price: number, discount: number = 0.1) {
return price - (price * discount);
}
In this example, we define a function called calculateDiscount()
that takes two parameters, price
of type number, and discount
of type number, which has a default value of 0.1
.
Inside the function, we calculate the discount and subtract it from the price to get the final discounted price.
¶Rest Parameters
In TypeScript, you can also use rest parameters to allow a function to accept any number of arguments of the same type. Rest parameters are denoted by an ellipsis … followed by the parameter name. Here’s an example:
function sum(...numbers: number[]) {
let total = 0;
for (let number of numbers) {
total += number;
}
return total;
}
In this example, we define a function called sum that accepts any number of arguments of type number and it’s array of numbers like this way - number[]
.
Inside the function, we loop through the arguments and add them together to get the total sum.
¶Conclusion
Functions are an essential part of any programming language, and TypeScript provides a wide range of features to make working with functions more convenient and efficient.
By understanding the different aspects of functions in TypeScript, including function declaration and parameters, return types, optional and default parameters, and rest parameters, you can write cleaner, more efficient, and more robust code.
TypeScript Variable, Annotation and Inference Learning
Interfaces in TypeScript - Learn TypeScript Interface in Depth
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development