-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.js
116 lines (116 loc) · 2.75 KB
/
types.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"use strict";
//sample code
var Greeter = /** @class */ (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
}());
var greeter = new Greeter("world");
var button = document.createElement('button');
button.textContent = "Say Hello 2";
button.onclick = function () {
alert(greeter.greet());
};
document.body.appendChild(button);
// types
// string
var str = "hello";
var stre = "explicit string";
stre = "explicit string changed";
// console.log(str + "" + stre);
//number
var num1 = 110;
var num2 = 10.5;
var num3 = 1023;
// console.log(num1,num2,num3);
num3 = 1500;
// console.log(num1,num2,num3);
//boolean
var bool = true;
var bool2 = false;
// console.log(bool,bool2);
//Any
var anyy;
anyy = "stringggg";
// console.log(anyy);
anyy = 100; // we can also reassign different type of value in any type of variable but not allowed in other types
// console.log(anyy);
var any2 = { val: 3, val2: 4 }; // we can also assign objects
// console.log(any2);
//Array
var arr = ["a", "b", "c", "d"];
// console.log(arr);
// console.log(arr[1]);
//Tuples
var tup1 = ["abc", 123];
// console.log(tup1);
// console.log(tup1[0]);
//enum
var color;
(function (color) {
color[color["red"] = 0] = "red";
color[color["green"] = 1] = "green";
color[color["blue"] = 2] = "blue";
})(color || (color = {}));
// console.log(color);
// console.log(color[1]);
// green
// console.log(color.red);
//0
// console.log(color.green);
//1
var color2;
(function (color2) {
color2[color2["pink"] = 0] = "pink";
color2[color2["yellow"] = 100] = "yellow";
color2[color2["grey"] = 101] = "grey";
color2[color2["black"] = 200] = "black";
color2[color2["brown"] = 201] = "brown";
})(color2 || (color2 = {}));
// console.log(color2);
// {0: "pink", 100: "yellow", 101: "grey", 200: "black", 201: "brown", pink: 0, yellow: 100, grey: 101, black: 200, brown: 201}
// console.log(color2[0]);
//pink
// console.log(color2[1]);
//undefined
// console.log(color2[100]);
//yellow
// console.log(color2.brown);
//201
// type objects
var obj = {
name: "amit",
age: 21
};
var obj2 = {
name: "amit",
age: 21
};
// console.log(obj2);
// console.log(obj2.name);
// union types ---- we can assign two type of values by using this
var uni;
uni = 25;
// console.log(uni);
uni = "hello";
// console.log(uni);
// never type ---- it always uses with error
function neverFun() {
throw new Error("error");
}
// type null
var vnull = null; // it creates var with null type not of any type
var var4;
// console.log(vnull);
// null
// console.log(var4);
//undefined
var var6 = null;
// console.log(var6);
// type of
console.log(typeof (uni));
console.log(typeof (var6));