-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
287 lines (168 loc) · 4.93 KB
/
index.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// This is a JavaScript Cheatsheet formatted nicely to help people learn JS easily.
// Written By Syed Shehroz Ali
// GitHub: github.com/sshehrozali
// To see the output please inspect the page source and goto console to see the output.
// Console in JS is basically like a terminal of any other programming language because JS runs under
// browser window only that's why console operates like a terminal for JS.
// Console API (To interact with console - developers tools/inspect elements)
console.warn("This is a warning message!");
console.error("This is an error message!");
console.assert(10 == (5+6));
// To print
// document.write("Hello World!");
// console.log("Hello World!");
// alert("Welcome to my JS tutorial website!");
/*
// Variables
var // Something old style or old syntax. Used in old JS
let // Temporary scope. Good memory management. It has scope under only {}
cont // Declares a constant variable whose value cannot be altered
*/
// Data types (Primitive)
console.log("\n\n\n---| DATA TYPES (Primitive) |---")
// String
var str = "I am a String";
console.log(str);
// Number
var num = 56;
var deci = 99.99;
console.log(num, deci);
// Boolean
var val1 = true;
var val2 = false;
console.log(val1, val2);
// Undefined
var undf = undefined; // Declaring explictly
var undF;
console.log(undf, undF);
// NULL
var n = null;
console.log(n);
// Symbol (High level JavaScript)
// Symbol('')
// Data types (Reference)
console.log("\n\n\n---| DATA TYPES (Reference) |---")
// Array
var arr = [1, 2.2, "I Love You"]
console.log("\nARRAY")
console.log(arr, arr[2])
// Object
var phonebook = {
"Shehroz": 123,
"Saad": 789,
"Sameer": 456
}
console.log("\nOBJECT")
console.log(phonebook);
// Operators
console.log("\n\n---| OPERATORS |---")
// Arthmetic Operators
var a = 99.9;
var b = 56.56;
console.log("\nARTHMETIC OPERATORS")
console.log("The value of a + b is", a + b);
console.log("The value of a - b is", a - b);
console.log("The value of a * b is", a * b);
console.log("The value of a / b is", a / b);
// Assignment Operators
console.log("\nASSIGNMENT OPERATORS")
var c = a;
c += b;
c -= b;
c *= b;
c /= b;
console.log(c);
// Comparison operators
console.log("\nCOMPARISON OPERATORS")
console.log(a > b);
console.log(a < b);
console.log(a >= b);
console.log(a <= b);
console.log(a != b);
// Logical Operators
console.log("\nLOGICAL OPERATORS")
console.log("\nAND OPERATOR")
console.log(false && false);
console.log(true && false);
console.log(false && true);
console.log(true && true);
console.log("\nOR OPERATOR")
console.log(false || false);
console.log(true || false);
console.log(false || true);
console.log(true || true);
console.log("\nNOT OPERATOR")
console.log(!false)
console.log(!true)
// Functions
function avg(a, b) {
ans = (a + b) / 2
return ans
}
c = avg(3, 5)
console.log("\n\n\n---| FUNCTIONS |---")
console.log("Average of 3 and 5 is", c)
// Conditions
console.log("\n\n\n---| CONDITIONS |---")
var a = 10;
var b = 10;
console.log("\nValue of a is", a, "\nValue of b is", b, "\nAnswer is ")
if (a == b){
console.log("Both values are equal!")
}
else{
console.log("Both values are not equal!")
}
if (a < b){
console.log("Yes! a is less than b");
if (b > a){
console.log("Obviously! b is greater than a");
}
}
else if (a > b){
console.log("No a is greater than b")
}
console.log("\n\n\n---| LOOPS |---")
var names = [
"shehroz", "saad", "sameer"
]
// For Loop
console.log("For Loop\n");
for (var i = 0; i < names.length; i++) {
console.log(names[i]);
}
// While Loop
console.log("\nWhile Loop\n");
let j = 0;
while (j < names.length) {
console.log(names[j]);
j = j + 1
}
// Do while Loop
console.log("\nDo While Loop\n");
let k = 0;
do {
console.log(names[k]);
k++;
}
while (k < names.length);
/*
// Misc
break // To terminate the whole loop
continue // To skip an iteration
*/
/*
// Array methods
MyArr = [1, 2, 3];
MyArr.length; // Check length of the array
MyArr.shift() // Removes first element from the array
MyArr.pop(); // Removes last item from the array
MyArr.push(4); // Adds new item to the array
MyArr.unshift(0); // Adds new item at first index of an array
MyArr.sort() // Sorts an array (sorts an array alphabetically)
MyArr.toString() // Returns an array as a complete string
// There are several many other methods for array. You can search through internet to explore more methods.
// This is not just an end. There are many other methods in JS which you need to explore on your own.
// There is rightly said that, there is no end to learn a language completely. Good Luck!
// Syed Shehroz Ali ©
*/