-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptionalVariable.ts
More file actions
42 lines (39 loc) · 1.13 KB
/
optionalVariable.ts
File metadata and controls
42 lines (39 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
type optionalUser = {
_id: string;
name: string;
age: number;
email: string;
isActive: boolean;
// Optional variable
address?: string;
// By using '?' we can mark a variable as optional, that means even if we don't pass the variable it won't throw an error.
}
// Now let's try this with a function
function optionalCreateUser(user: optionalUser): optionalUser {
return user;
}
// Now let's try to pass a user without the address variable
console.log(optionalCreateUser({ _id: '1235', name: 'Ashiq', age: 18, email: 'ashiqtasdid5@gmail.com', isActive: true }))
/*
Here is the result:
{
_id: '1235',
name: 'Ashiq',
age: 18,
email: 'ashiqtasdid5@gmail.com',
isActive: true
}
*/
// Now let's try to pass a user with the address variable
console.log(optionalCreateUser({ _id: '1235', name: 'Ashiq', age: 18, email: 'ashiqtasdid5@gmail.com', isActive: true, address: 'Mars' }));
/*
Here is the result:
{
_id: '1235',
name: 'Ashiq',
age: 18,
email: 'ashiqtasdid5@gmail.com',
isActive: true,
address: 'Mars'
}
*/