Skip to content

Commit 18916d0

Browse files
committed
Functions in ts
1 parent e6f56ed commit 18916d0

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

TS/Basics/functions.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// FUNCTIONS
2+
3+
function addTwo(num: number): number {
4+
return num + 2;
5+
// return "hello" // error
6+
}
7+
8+
function getUpper(word: string){
9+
return word.toUpperCase();
10+
}
11+
12+
function signUpuser(name: string, email: string, isPaid: boolean){}
13+
14+
let loginUser = (name: string, email: string, isPaid: boolean = false) => {} // entering the default value
15+
16+
console.log(addTwo(5));
17+
console.log(getUpper("hello"));
18+
signUpuser("Tony", "ironman@avengers.com", false);
19+
loginUser("Peter", "pete@avengers.com")
20+
21+
22+
// function getVal(myVal: number){
23+
// if (myVal > 5) {
24+
// return true
25+
// }
26+
// return "200 OK"
27+
// }
28+
29+
const getGreetings = (greet: string): string => {
30+
return "";
31+
}
32+
33+
const heroes = ["thor", "spiderman", "ironman"]
34+
// const heroes = [1, 2, 3]
35+
36+
heroes.map((hero): string => {
37+
return `Hero is ${hero}`
38+
})
39+
40+
// void represents the return value of functions which don’t return a value. It’s the inferred type any time a function doesn’t have any return statements, or doesn’t return any explicit value from those return statements
41+
function consoleError(errorMsg: string): void{
42+
console.log(errorMsg);
43+
}
44+
45+
// The never type represents values which are never observed. In a return type, this means that the function throws an exception or terminates execution of the program.
46+
function handleError(errorMsg: string): never{
47+
throw new Error(errorMsg);
48+
}
49+
50+
51+
export{}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy